简介
Logrotate 程序是一个日志文件管理工具。用来把旧的日志文件重新命名,并创建新的日志文件,这个过程叫做“转储”。可以根据日志文件的大小,也可以根据其天数来转储,这个过程通过 cron 程序来执行,比如系统每天的定时任务会执行一次logrotate操作来完成系统日志的转储。logrotate 的默认配置文件是 /etc/logrotate.conf,一般自定义一个新的配置文件来完成相应日志切割管理。
logrotate [OPTION...] <configfile>
-d, --debug 测试,但不会真正运行(已包含-v选项)
-f, --force 强制对文件轮替
-m, --mail=command 后接参数,参数是发邮件命令(替代`/bin/mail`)
-s, --state=statefile 状态文件的路径
-v, --verbose 输出轮替过程中的信息到标准输出
配置参数详解
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期,没有这个配置项会附加一个小数点加一个数字序号.
dateformat 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期
Logrotate的介绍
显而易见,Logrotate是基于CRON来运行的,其脚本是「/etc/cron.daily/logrotate」
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
实际运行时,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
# 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 -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
Nginx配置文件例子
#vim /etc/logrotate.d/nginx
/www/logs/*.log {
daily
dateext
#compress
rotate 30
notifempty
sharedscripts
postrotate
if [ -f /usr/local/nginx/nginx.pid ]; then #设置rotate之后nginx需重新载入配置文件,否则nginx不会将日志写入新的日志文件中
service nginx restart > /dev/null 2&>1
fi
endscript
}
如果你等不及cron,可以通过如下命令来手动执行:
logrotate -f /etc/logrotate.d/nginx
当然,正式执行前最好通过Debug选项来验证一下,这对调试也很重要:
logrotate -d -f /etc/logrotate.d/nginx
php-fpm配置文件例子
#vim /etc/logrotate.d/php-fpm
/usr/local/php/var/log/*.log {
daily
dateext
#compress
rotate 30
notifempty
sharedscripts
postrotate
service php-fpm restart > /dev/null 2>&1
endscript
}
前面Nginx的例子里声明日志文件的时候用了星号通配符,也就是说这里可能涉及多个日志文件,比如:access.log和error.log。说到这里大家或许就明白了,sharedscripts的作用是在所有的日志文件都轮转完毕后统一执行一次脚本。如果没有配置这条指令,那么每个日志文件轮转完毕后都会执行一次脚本。
rotate和maxage的区别是什么?
它们都是用来控制保存多少日志文件的,区别在于rotate是以个数为单位的,而maxage是以天数为单位的。如果我们是以按天来轮转日志,那么二者的差别就不大了。
为什么生成日志的时间是凌晨四五点?
前面我们说过,Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,具体可以查询CRON的配置文件「/etc/crontab」,可以手动改成如23:59等时间执行:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
59 23 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
如果使用的是新版CentOS,那么配置文件为:/etc/anacrontab。
[localhost ~]# more /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