几天来,我一直在网上搜索如何设置我的服务器以自动轮换我的团队最近发布的 Rails 应用程序的日志。我已经让自己能够跑步sudo logrotate -f /etc/logrotate.conf
了,但是当然,谁愿意一直这样做呢?
应用程序日志的配置文件的内容(我想添加更多,但当我还不能旋转一个文件时,我认为不需要):
/path/to/app/production.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
我已经验证该/etc/logrotate.conf
文件包含这一行:
include /etc/logrotate.d
但这是我不太确定去哪里的部分。我发现了许多不同的方法来实际自动化该过程,但似乎都没有奏效。作为记录,我已验证服务器已anacron
安装命令,但我不知道如何为我自己的任何进程配置它。此外,root 在服务器上还没有 crontab(我们不需要它),我不确定这是否比/etc/crontab
. 在/etc/crontab
文件中,我添加了:
15 0 * * * root cd / && run-parts --report /etc/cron.daily
但我见过其他人使用
15 0 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
后者是更好的选择吗?为什么?如果是这样,我如何确保它有效?同样,我不知道如何为手头的任务设置 anacron。
最后,这里是/etc/cron.daily/logrotate
文件之前的内容:
/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
经过一些研究,我用这个替换了它(我理解得更好):
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
有人可以向我解释第一个配置在做什么,这两个选项中哪个更好?我不确定为什么我必须强制这个过程才能让它运行。也许/etc/crontab
不像我想的那样工作?