Linux

Cron 程序在 Logrotate 中無限期掛起

  • October 21, 2016

我目前有一台伺服器打算每天輪換它的日誌。它logrotate本身很可能是讓我的cron程序掛起的那個。

每次cron執行後我都必須手動終止該程序,以便第二天再次執行。

[root@localhost ~]# ps aux | grep cron
root     15364  0.0  0.0 116884  1304 ?        Ss   Sep12   0:03 crond
root     17898  0.0  0.0  19060   972 ?        Ss   Sep13   0:00 /usr/sbin/anacron -s
root     17911  0.0  0.0 106112  1272 ?        SN   Sep13   0:00 /bin/bash /usr/bin/run-parts /etc/cron.daily
root     17915  0.0  0.0 106112  1140 ?        SN   Sep13   0:00 /bin/sh /etc/cron.daily/logrotate
root     17916  0.0  0.0 105968   900 ?        SN   Sep13   0:00 awk -v progname=/etc/cron.daily/logrotate progname {?????   print progname ":\n"?????   progname="";????       }????       { print; }
root     22439  0.0  0.0 103320   856 pts/0    S+   09:53   0:00 grep cron

從日誌中可以看出,該作業被另一個anacron.

[root@localhost ~]# tail /var/log/cron
Sep 16 09:01:01 localhost run-parts(/etc/cron.hourly)[22374]: starting 0anacron
Sep 16 09:01:01 localhost anacron[22386]: Anacron started on 2016-09-16
Sep 16 09:01:01 localhost anacron[22386]: Job `cron.daily' locked by another anacron - skipping
Sep 16 09:01:01 localhost anacron[22386]: Normal exit (0 jobs run)
Sep 16 09:01:01 localhost run-parts(/etc/cron.hourly)[22388]: finished 0anacron
Sep 16 09:10:01 localhost CROND[22393]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 16 09:20:01 localhost CROND[22400]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 16 09:30:01 localhost CROND[22409]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 16 09:40:01 localhost CROND[22421]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 16 09:50:01 localhost CROND[22432]: (root) CMD (/usr/lib64/sa/sa1 1 1)

一直在檢查配置文件logrotate.d,一切正常,什麼可能讓我的 cron 掛起?

顯然有人在 squid 配置文件中留下了logrotate以下配置:

/dir/user/logs/squid/* {
   weekly
   rotate 5
   compress
   notifempty
   missingok
   sharedscripts
   postrotate
     # Asks squid to reopen its logs. (log_rotate 0 is set in squid.conf)
     # errors redirected to make it silent if squid is not running
     /usr/sbin/squid -k rotate 2>/dev/null
     # Wait a little to allow Squid to catch up before the logs is compressed
     sleep 1
   endscript
}

該配置中的問題是它正在旋轉每個文件,包括已經壓縮的文件,給我留下了數百萬個多次壓縮的文件及其後代。配置應該包括*.log*而不是全部*

/dir/user/logs/squid/*.log

--debug通過使用or選項從命令行手動執行 logrotate 命令來調試 logrotate 配置,--verbose以查看它掛起的位置和原因。

引用自:https://serverfault.com/questions/803476