logrotate命令–管理日志文件
使用logrotate指令,可以让你轻松管理系统所产生的日志文件。它提供自动替换,压缩,删除和邮寄日志文件,每个日志文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。 logrotate 的主要功能是将旧的日志文件移动成旧文件,并且重新建立一个新的空的文件。它的执行结果如下图示意: 
您必须自行编辑,指定配置文件,预设的配置文件存放在/etc目录下,文件名称为logrotate.conf。 logrotate.conf是主要的参数文件,至于logrotate.d是一个目录,该目录里面的所有文件都会被logrotate.conf文件主动读入,若logrotate.d/里的文件没有指定详细设置,则以/etc/logrotate.conf这个文件里配置的默认值为准。
logrorare一定程度上可以简化对会生成大量日志文件的系统的管理。logrotate可以实现自动轮替、删除、压缩和mail日志的功能。
常用参数:
--help
在线帮助
-d
详细显示指令执行过程,便于排错或了解程序执行的情况
-f
强行启动日志文件维护操作,纵使logrotate指令认为没有需要亦然
-s
使用指定的状态文件
-v
显示指令执行过程
-usage
显示指令基本用法
日志轮替的流程基于crond服务
crond服务加载/etc/cron.d/0hourly --->在每小时的01分执行/etc/cron.hourly/0anacron --->执行anacron --->根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly --->执行/etc/cron.daily/下的logrotate脚本 --->执行logrotate --->根据/etc/logrotate.conf配置执行/etc/logrotate.d/下的脚本
管理文件为/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 # 随机的延迟时间,表示最大45分钟
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 # 在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
# period in days 是表示1天、7天、1个月执行一次
# delay in minutes 是延迟的分钟数
# nice设置优先级为10,范围为-20(最高优先级)到19(最低优先级)
# run-parts 是一个脚本,表示会执行它后面目录里的脚本/etc/logrotate.d/目录下的配置示例
# tomcat
/jpdata/kubernetes/logs/unl/*/catalina.out {
daily
rotate 30
dateext
#create 0644 test_release test
dateformat -%Y-%m-%d
extension .out
missingok
notifempty
nocompress
su test_release test
copytruncate
}
/data/kubernetes/logs/unl/*/*/*.err {
daily
rotate 30
dateext
#create 0644 test_release test
missingok
notifempty
nocompress
su test_release test
copytruncate
}Nginx使用logrotate轮转日志
Nginx 在运行过程中会持有打开的日志文件描述符,即使日志文件被轮转(即原日志文件被重命名或压缩),Nginx 仍然会继续向原文件写入日志,而不会写入新的日志文件。所以在使用 logrotate 轮转 Nginx 的日志文件时,需要通过发送信号给 Nginx 主进程来通知它重新打开日志文件/bin/kill -USR1 cat /data/logs/nginx/nginx.pid 2>/dev/null 2>/dev/null
/data/logs/nginx/*.log {
daily
rotate 7
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/bin/kill -USR1 `cat /data/logs/nginx/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}配置文件可用参数说明
daily
每天轮替一次
weekly
每周轮替一次
monthly
每月轮替一次
yearly
每年轮替一次
rotate
保留几个轮替日志文件
ifempty
不论日志是否空,都进行轮替
notifempty
若日志为空,则不进行轮替
create
旧日志文件轮替后创建新的日志文件,跟copytruncate不能同时使用
copytruncate
用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate
备份日志文件但是不截断
size
日志达到多少后进行rotate , 例如:1M 1G
minsize
文件容量一定要超过多少后才进行rotate
nocompress
轮替但不进行压缩
compress
压缩轮替文件
dateext
轮替旧日志文件时,文件名添加-%Y %m %d形式日期,可用dateformat选项扩展配置。
nodateext
旧日志文件不使用dateext扩展名,后面序数自增如"*.log.1"
dateformat
只允许%Y %m %d和%s指定符。注意:系统时钟需要设置到2001-09-09之后,%s才可以正确工作
sharedscripts
作用域下文件存在至少有一个满足轮替条件的时候,执行一次prerotate脚本和postrotate脚本。
prerotate/endscript
在轮替之前执行之间的命令,prerotate与endscript成对出现。
postrotate/endscript
在轮替之后执行之间的命令,postrotate与endscript成对出现。
olddir
将轮替的文件移至指定目录下
missingok
如果日志文件不存在,继续进行下一个操作,不报错
maxsize
当文件大小超过 maxsize,或者超过指定时间间隔就会转储日志。3.8以前的版本没这个选项
create和copytruncate为互斥的参数配置。 create的实现思路是将原日志文件进行重命名,再以原日志文件名创建新日志文件。不过需要注意的是,如果当前进程不支持重新打开日志文件,新生成的日志会出现无法正常写入到新日志文件的情况,这里就需要重启进程达到重新打开日志文件的目的。 为了解决当前进程不支持重新打开日志文件的场景,我们这里可以通过配置参数copytruncate代替create。 copytruncate的实现思路是将原日志文件中的内容拷贝到新日志文件中,然后对原日志文件内容进行清空操作,因为不存在重新打开日志文件的需求。不过使用copytruncate也存在一定的风险,即由于拷贝和清空是两个操作步骤,操作存在时间间隔,因为从拷贝完成到清空文件之间生成的日志信息会丢失。
minsize maxsize 和 size 的区别
minsize: 当文件大小超过 minsize,并且超过指定时间间隔才会转储日志。例如:minsize 50M,hourly 表示当文件大小超过 50MB 并且自上次转储后过去了一个小时,才会进行转储。
maxsize: 当文件大小超过 maxsize,或者超过指定时间间隔就会转储日志。例如: maxsize 50M,hourly 表示当文件大小超过 50MB 或者自上次转储后过了一个小时,就会进行转储。
size: 只要文件大小超过 size 就会进行转储,与时间无关。
验证配置是否可用
logrotate -dv /PATH/CONFIG_FILE周期性定时任务配置文件在/etc/crontab
cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#最后更新于