故障现象
有个系统服务器重新搭建部署应用后发现nginx日志不轮询。手动执行logrotate命令能成功,但是自动执行不成功。
排查问题思路如下:
1、先看下配置文件是否正确,可以先手动执行
logrotate /etc/logrotate.conf
2、看下logrotate执行的结果
# cat /var/lib/logrotate.status logrotate state -- version 2 "/var/log/ConsoleKit/history" 2020-8-1 "/var/log/yum.log" 2020-7-28 "/var/log/salt/key" 2020-8-8 "/app/logs/http_error.log" 2020-9-18 "/var/log/dracut.log" 2020-7-28 "/var/log/salt/master" 2020-8-8 "/var/log/wtmp" 2020-7-28 "/app/logs/https_error.log" 2020-9-18 "/var/log/spooler" 2020-9-13 "/var/log/btmp" 2020-9-1 "/var/log/puppet/*log" 2020-9-17 "/var/log/xferlog" 2020-9-5 "/app/logs/http_access_combined.log" 2020-9-18 "/var/log/maillog" 2020-9-13 "/var/log/secure" 2020-9-13 "/var/log/salt/minion" 2020-9-13 "/app/logs/http_access.log" 2020-9-18 "/app/logs/https_access.log" 2020-9-18 "/var/log/messages" 2020-9-13 "/var/account/pacct" 2020-7-28 "/var/log/cron" 2020-9-13 "/var/log/vsftpd.log" 2020-9-5
3、logrotate是crontab执行的,涉及到以下俩个文件
# cat /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
有的服务还会在crontab设置定时执行,我的没有这个文件。
cat /etc/cron.d/nginx 55 23 * * * root sleep `perl -e "print int(rand(120))"` && /usr/sbin/logrotate -v -f /usr/local/nginx/conf/logrotate.conf
4、由于并没有发现问题,我的日志轮询不生效,为了先解决问题,只能自己写个crontab了
# 日志轮询 0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf
参考
Nginx日志归档logrotate配置失效不执行的问题排查记录 - 服务端开发 http://tabalt.net/blog/nginx-logrotate-execution-failure/