logrotate是一个日志管理程序,用来把旧的日志文件删除(备份),并建立新的日志文件,这个过程称为“转储”。咱们能够根据日志的大小,或者根据其使用的天数来转储。 |
logrotate 的执行由crond服务实现。在/etc/cron.daily目录中,有个文件logrotate,它其实是个shell script,用来启动logrotate。logrotate程序天天由cron在指定的时间(/etc/crontab)启动。所以,使用ps是没法查看到logrotate的。若是它没有起来,就要查看一下crond服务有没有在运行。在执行logrotate时,须要指定其配置文件/etc/logrotate.conf。这 个配置文件的注释写得很清楚,没有必要再罗嗦了。只想强调下面这行,它的做用包含存放在/etc/logrotate.d目录下面的配置文件,不可或缺。 若是你安装了一个新的服务,它的日志转储的规则能够创建一个专门的配置文件,放在/etc/logrotate.d下面。它其实也由于下面的这句话,在 logrotate服务启动时被读取。php
每一个存放在/etc/logrotate.d目录里的文件,都有上面格式的配置信息。在{}中定义的规则,若是与logrotate.conf中的冲突,以/etc/logrotatate.d/中的文件定义的为准。node
Logrotate的运行分为三步,判断系统的日志文件,创建转储计划以及参数,经过cron daemon 运行下面的代码是 Red Hat Linux缺省的crontab 来天天运行logrotate。logrotate启动脚本放在/etc/cron.daily/logrotate中.linux
#!/bin/sh test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.conf
可人工执行命令进行测试:nginx
/usr/sbin/logrotate -f /etc/logrotate.conf
看一下主配置文件/etc/logrotate.conf的参数解释:shell
# 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 #rotate后,建立一个新的空文件 # 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 { #定义/var/log/wtmp这个日志文件; monthly #每个月轮转一次,取代了上面的全局设定的每周轮转一次; minsize 1M #定义日志必需要大于1M大小才会去轮转; create 0664 root utmp #新的日志文件的权限,属主,属主; rotate 1 #保留一个,取代了上面的全局设定的保留四个; } /var/log/btmp { #定义/var/log/btmp这个日志文件; missingok #若是日志丢失, 不报错; monthly create 0600 root utmp rotate 1 }
若是在对应的文件下没有设定对应的参数,那么此日志会继承全局配置参数,如:/var/log/btmp文件没有指定monthly,那么机会继承全局参数weekly进行日志轮换了。tomcat
daily #指定转储周期为天天 weekly #指定转储周期为每周; monthly #指定转储周期为每个月; rotate count #指定日志文件删除以前转储的次数,0指没有备份,5指保留5个备份; compress #经过gzip压缩转储之后的日志; nocompress #不须要压缩时,用这个参数; delaycompress #延迟压缩,和compress一块儿使用时,转储的日志文件到下一次转储时才压缩; nodelaycompress #覆盖delaycompress选项,转储同时压缩; copytruncate #用于还在打开中的日志文件,把当前日志备份并截断; nocopytruncate #备份日志文件可是不截断; create mode owner group #转储文件,使用指定的文件模式建立新的日志文件; nocreate #不创建新的日志文件; errors address #专储时的错误信息发送到指定的Email地址; ifempty #即便是空文件也转储,这个是logrotate的缺省选项; notifempty #若是是空文件的话,不转储; mail address #把转储的日志文件发送到指定的E-mail地; nomail #转储时不发送日志文件; olddir directory #转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统; noolddir #转储后的日志文件和当前日志文件放在同一个目录下; prerotate/endscript #在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行; postrotate/endscript #在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行; tabootext [+] list #让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave,v,和~ ; size size #当日志文件到达指定的大小时才转储,Size能够指定bytes(缺省)以及KB(sizek)或者MB(sizem); postrotate #日志轮换事后指定指定的脚本,endscript参数表示结束脚本; sharedscripts #共享脚本,下面的postrotate中的脚本只执行一次便可;
以上参数均可以已定义在全局配置,或者指定为某个日志文件对的配置,但注意使用时参数之间不要冲突。php-fpm
一个新系统准备上线前,应该作好如下日志的配置,配置到/etc/logrotate.d/目录下便可。post
/var/log/syslog { rotate 7 #保留7份日志文件; daily #天天轮换一次; missingok #若是日志丢失,不报错; minsize 10M #定义日志必须大于10M才会去轮换; notifempty #若是是空文件就不进行转存; compress #压缩日志; delaycompress #延迟压缩,和compress一块儿使用时,转储的日志文件到下一次转储时才压缩; postrotate #轮换事后重启rsyslog服务; invoke-rc.d rsyslog rotate > /dev/null endscript #结束脚本; } /var/log/mail.info /var/log/mail.warn /var/log/mail.err /var/log/mail.log /var/log/daemon.log /var/log/kern.log /var/log/auth.log /var/log/user.log /var/log/lpr.log /var/log/cron.log /var/log/debug /var/log/messages { rotate 4 weekly missingok notifempty compress delaycompress sharedscripts #共享脚本,下面的postrotate脚本只执行一次便可; postrotate invoke-rc.d rsyslog rotate > /dev/null endscript }
在定义日志文件时也可使用通配符,但最好少用通配符,因会它会包括已经切换过的日志,要用的话最好在*号后加上扩展名, 如*.log。测试
通常除了这些系统日志须要配置一下,还有一些应用日志以及组件日志可能也须要配置,如Nginx、php、tomcat、zabbix-agent等。this
/var/log/nginx/*log { daily rotate 10 missingok notifempty compress sharedscripts su root postrotate /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || : endscript }
/var/log/php-fpm/*log { missingok notifempty sharedscripts delaycompress create 0664 www www postrotate /bin/kill -SIGUSR1 `cat /var/run/php-fpm/php-fpm.pid 2>/dev/null` 2>/dev/null || true endscript }