Logrotate 在錯誤的時間/時間運行

Logrotate 在錯誤的時間/時間運行

我的文件中有以下條目,/etc/crontab以確保每日 cronjobs 在凌晨 4:25 運行。這是daily該文件內的唯一條目:

25 4 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

我的內容/etc/cron.daily/包括logrotate

 # ls -l /etc/cron.daily/
...
-rwxr-xr-x 1 root root  377 Jan 21  2019 logrotate

這是下面的 logrotate 配置/etc/logrotate.d/

/var/log/remote/custom/*.log
{
        rotate 180
        daily
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
    # force rsyslog to refresh file descriptor
        postrotate
                /usr/lib/rsyslog/rsyslog-rotate
        endscript
}

為什麼當我檢查 的內容時 /var/lib/logrotate/status,我看到日誌檔案在午夜而不是凌晨 4:25 輪換?

"/var/log/remote/custom/cust.log" 2020-11-4-0:0:39

作業系統:Ubuntu 20.04 LTS 核心:Linux 5.4.0-40-generic

答案1

您似乎忽略了 /etc/anacrontab,其中 anacron 發現何時評估 /etc/cron.daily (和其他)。

另外,這也是我遠離 systemd 的眾多原因之一。檢查 /etc/cron.daily/logrotate 的第一行:

# less -X  /etc/cron.daily/logrotate                        
#!/bin/sh

# skip in favour of systemd timer
if [ -d /run/systemd/system ]; then
    exit 0
fi

答案2

為了德班用戶,您可能會認為它運行在 6.25 (/etc/crontab),但自上幾個版本以來,它運行在 00.00 並且配置為系統

舊的 /etc/crontab :

25 6 * * * 根測試 -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

如果你想改變 systemd 的執行時間,你必須編輯這個檔案: /etc/systemd/system/timers.target.wants/logrotate.timer

預設是:

[Timer]
OnCalendar=daily

如果你想每天 6.20 運行它:

[Timer]
OnCalendar=*-*-* 6:20

(然後我重新啟動了系統,但我不確定這是否有必要!)

答案3

編輯時要非常小心/etc/systemd/system/timers.target.wants/logrotate.timer- 該文件是/lib/systemd/system/logrotate.timer.

如果文件意外地從符號連結變更為常規文件(如果您使用(例如)sed 作為嵌入式系統安裝腳本的一部分來編輯此文件,則計時器將被忽略。問我怎麼知道的!

相關內容