正常情況下,/var/log 下的日誌每 7 天會切分一次,舊檔改名為 xxx.1,累積一個月後自動刪除。但有些機器偏偏不切,日誌一路長大直到塞爆磁碟。
日誌切分是由 /etc/cron.daily/logrotate 觸發,它的全域設定檔是 /etc/logrotate.conf:
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
include /etc/logrotate.d
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}這個檔案決定切分週期與保留份數;至於每個服務各自的切分規則,則放在 /etc/logrotate.d 目錄下的個別腳本裡,apache、mysql、resin 等都在其中(郵件日誌 /var/log/maillog 由 syslog 寫入,規則另計)。
直接手動跑一次,看它吐出什麼錯誤:
/etc/cron.daily/logrotate
若出現類似訊息:
error: httpd:1 duplicate log entry for /var/log/httpd/access_log
代表 /etc/logrotate.d 底下有重複的切分項目。本例是使用者自行用 rpm 裝了 httpd,於是 apache 與 httpd 兩份規則同時存在,logrotate 便直接失敗退出。
把重複的 /etc/logrotate.d/httpd 移開即可。比較穩健的習慣是拿一台乾淨的機器做對照,逐一確認是否有多餘或衝突的切分設定;順便檢查 cron.daily 的執行權限,以及 logrotate 執行紀錄(通常可在 /var/log/cron 中找到蛛絲馬跡)。