1. 程式人生 > >cron 和anacron 、日誌轉儲的周期任務

cron 和anacron 、日誌轉儲的周期任務

lis kcon 進行 配置文件 ttr 開機自動啟動 space net uri

技術分享圖片 一、cron是開機自動啟動的 [root@localhost ~]# chkconfig --list | grep "cron" crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off 技術分享圖片 可以看到 crond 在系統的3級別是自動啟動的。3級、5級是常用的級別
[root@localhost ~]# chkconfig --list | grep "cron"
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off

另外可以在 /etc/init.d 目錄中看到自動啟動的服務。這些服務可以通過service 來啟動和關閉 技術分享圖片
[root@localhost ~]#
cd /etc/init.d [root@localhost init.d]# ls abrt-ccpp certmonger halt mcelogd nfs-rdma quota_nld rpcsvcgssd sysstat abrtd cgconfig ip6tables mdmonitor ntpd rdisc rsyslog udev-post abrt-oops cgred iptables messagebus ntpdate rdma sandbox winbind acpid cpuspeed irqbalance netconsole numad restorecond saslauthd ypbind atd crond kdump netfs oddjobd rngd single auditd cups killall network portreserve rpcbind smartd autofs functions lvm2
-lvmetad nfs postfix rpcgssd sshd blk-availability haldaemon lvm2-monitor nfslock psacct rpcidmapd sssd

二、cron 開機啟動後感什麽工作 man cron Cron should be started from /etc/rc.d/init.d or /etc/init.d (定義cron 開機自啟) 1、查看 /var/spool/cron Cron searches /var/spool/cron ,看有沒有我們定義的crontab 周期任務 通過crontab -e 創建的周期任務在 此目錄顯示,同過crond 服務調用 2-1、查看 /etc/cron.d directory,目錄

[root@localhost init.d]#
cd /etc/cron.d [root@localhost cron.d]# ls 0hourly raid-check sysstat [root@localhost cron.d]# cat 0hourly SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ 01 * * * * root run-parts /etc/cron.hourly

每小時一分 執行 /etc/cron.hourly 目錄下的腳本

[root@localhost cron.d]# cd /etc/cron.hourly/
[root@localhost cron.hourly]# ls
0anacron mcelog.cron
[root@localhost cron.hourly]# cat 0anacron #!/bin/bash # Skip excecution unless the date has changed from the previous run if test -r /var/spool/anacron/cron.daily; then /var/spool/anacron/cron.daily文件中 存放cron 上次啟動時間 如果當期的時間和文件中的時間一致,表示本次已經執行完畢,無需再次執行 day=`cat /var/spool/anacron/cron.daily` fi if [ `date +%Y%m%d` = "$day" ]; then exit 0; fi # Skip excecution unless AC powered if test -x /usr/bin/on_ac_power; then /usr/bin/on_ac_power &> /dev/null if test $? -eq 1; then exit 0 fi fi /usr/sbin/anacron -s 開機後的第一個 一分鐘 (每個小時的第一分鐘)開啟 anacron 服務 anacron 由 cron 啟動

2-2、查看 /etc/anacrontab 中的內容 cat
Cron also searches for /etc/anacrontab
[root@localhost cron]# cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron
anacron的配置文件
# See anacron(8) and anacrontab(5) for details.
 
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45 隨機延遲時間 0-45 分鐘
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 執行時間 3點到 22點
幾天執行一次 固定延遲幾分鐘 nice 執行的命令
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

技術分享圖片 分別按 日期 周 月份 執行 /etc/cron.daily /etc/cron.weekly /etc/cron.monthly 目錄中的腳本 技術分享圖片 在這裏 看到了 logrotate 日誌轉儲程序,每天執行一次。、   
[root@localhost cron.daily]# cat logrotate 
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf 執行logrotate 轉儲命令,讀 
取/etc/logrotate.conf 主要配置文件(包含次要配置文件/etc/logrotate.d)按照 配置文
件的規則對日誌進行轉儲
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
 /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
三、日誌的轉儲是通過 anacron 執行的周期任務。是開機後 cron 檢查文件和目錄後 開啟 anacron 服務,在執行 anacron 中定義的周期任務的文件 logrotate 對日誌進行轉儲 技術分享圖片

cron 和anacron 、日誌轉儲的周期任務