我设置了以下 logrotate 任务来压缩 cron 作业生成的日志
/var/test/mytest.log
{
create 0644 user user
rotate 30
copytruncate
daily
compress
missingok
notifempty
}
logrotate 输出文件
mytest.log 0 Byte
mytest.log-20230417.gz 53453 Byte
上述脚本的问题在于它也存档了当天的脚本并留下了空日志。我们怎样才能避免最近这种奇怪的情况呢?
今天我必须为我的集群解决这个确切的问题。它适用于我古老的 logrotate 版本 3.8.6(我相信它是 2013 年的)。
/var/test/mytest.log
{
# run this script for each and every log file
nosharedscripts
# check logrotate the file if it was modified more last n*24 hours ago
prerotate
logfile=$1
find $logfile -mtime 1 | grep . > /dev/null
exit $?
endscript
create 0644 user user
rotate 30
copytruncate
daily
compress
missingok
notifempty
}
确保您也有
nosharedscripts
(因为它将为找到的每个日志文件运行此预旋转脚本)。