【logrotate】linux定时文件切割(解决openresty单个日志文件过大问题)

本文介绍了如何使用logrotate工具对OpenResty产生的日志进行定期切割,以节省磁盘空间并简化问题排查。详细讲解了logrotate的基本概念、配置参数、CRON定时执行机制,以及在openresty中的定制配置实例。

搭建openresty的web服务之后,日志文件随着时间和业务量会逐渐增大,既占用磁盘空间,又不便于日志查看,调查问题。这个时候,就可以使用logrotate做下定时日志切割啦,既方便又省事。

一、logrotate介绍

logrotate程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。Linux系统默认安装logrotate工具,它默认的配置文件在:/etc/logrotate.conf ,/etc/logrotate.d/,Logrotate是基于CRON来运行的,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。实际运行时,Logrotate会调用配置文件/etc/logrotate.conf。

(1)关于CRON服务的定时周期执行时间,centos系统请查看/etc/anacrontab这个文件,如下:

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

START_HOURS_RANGE=3-22 表示作业的执行时间范围3点-22点之间执行,基本就是3点开始的,再就是延时。

RANDOM_DELAY=45,这个是随机最大延时时间,具体还是要看分任务的延时,比如daily的delay in minutes配置是5,那就是至少延时5分钟了,也就是每日定时任务执行时间会是在3:05之后。

二、logrotate的配置

配置的路径是/etc/logrotate.conf,文件内容如下:

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值