实际使用
在目录/etc/logrotate.d下新建一个web-log/home/zmk/logs/access.log /home/zmk/logs/error.log {
notifempty
daily
rotate 10
dateext
create 0777 zmk zmk
}
测试sudo /usr/sbin/logrotate -f /etc/logrotate.d/web-log
发现新生产的 access.log 和 error.log nginx 没有权限写入,什么鬼?
加上重启命令/home/zmk/logs/access.log /home/zmk/logs/error.log {
notifempty
daily
rotate 10
dateext
create 0777 zmk zmk
sharedscripts
postrotate
if [ -f /home/zmk/lib/tengine/logs/nginx.pid ]; then
kill -USR1 `cat /home/zmk/lib/tengine/logs/nginx.pid`
fi
endscript
}
再次测试,通过。如果不重启,则还是往旧的文件中在写入。
原理分析
logrotate默认被执行是通过/etc/crontab里面的02 4 * * * root run-parts /etc/cron.daily
查看/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
而在/etc/logrotate.conf里面有包含了/etc/logrotate.d目录下所有的计划任务。