尝试解决logrotate无法自动轮询日志的办法
2025年5月31日大约 1 分钟
尝试解决logrotate无法自动轮询日志的办法
现象说明:
使用logrotate轮询nginx日志,配置好之后,发现nginx日志连续两天没被切割,这是为什么呢??
然后开始检查日志切割的配置文件是否有问题,检查后确定配置文件一切正常。
于是怀疑是logrotate预定的cron没执行,查看了cron的日志,发现有一条Dec 7 04:02:01 www crond[18959]: (root) CMD (run-parts /etc/cron.daily)这样的日志,证明cron在04:02分时已经执行/etc/cron.daily目录下的程序。
接着查看/etc /cron.daily/logrotate(这是logrotate自动轮转的脚本)的内容:
[root@huanqiu_test ~]# cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
没有发现异常,配置好的日志轮转操作都是由这个脚本完成的,一切运行正常,脚本应该就没问题。
直接执行命令:
[root@huanqiu_test ~]# /usr/sbin/logrotate /etc/logrotate.conf
这些系统日志是正常轮询了,但nginx日志却还是没轮询
接着强行启动记录文件维护操作,纵使logrotate指令认为没有需要,应该有可能是logroate认为nginx日志太小,不进行轮询。
提示
故需要强制轮询,即在/etc/cron.daily/logrotate脚本中将 -t 参数替换成 -f 参数
[root@huanqiu_test ~]# cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -f logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
最后最后重启下cron服务:
[root@huanqiu_test ~]# /etc/init.d/crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
systemctl restart crond.service