2010年11月2日星期二

Cron和Anacron

今天查看Ubuntu下系统的cron任务是如何执行的,发现了很纠结的事情。
系统的cron文件/etc/crontab的相关内容是这样的:

17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

第一行确保了/etc/cron.hourly中的任务可以运行,但是这个目录是空的,没任务。下面三行是说,如果/usr/sbin/anacron不能执行,才执行/ect/cron.daily、/etc/cron.weekly和/etc/cron.monthly里面的任务。但是anancron是装上的,所以后面三行中的任务是没法执行的。
然后摸索了下,看看anacron是个什么东东。它的系统任务是/etc/anacrontab,这个文件的内容如下:

1 5 cron.daily nice run-parts --report /etc/cron.daily
7 10 cron.weekly nice run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly nice run-parts --report /etc/cron.monthly

看到这里才明白了,原来是通过anacron来运行cron的任务。就是说实际的任务脚本在cron的相关目录里面,而调用是要通过anacron的。难怪我要说纠结呢。
anacron的手册里面也指出了anacron的这种用法:

Anacron can be used to execute commands periodically, with a
frequency specified in days. Unlike cron(8), it does not assume that
the machine is running continuously. Hence, <b>it can be used on
machines that aren't running 24 hours a day, to control daily, weekly,
and monthly jobs that are usually controlled by cron.</b>

可以看出anacron是辅助cron任务更好地得以执行的。把anacron删除了这些任务还是可以运行,但是必须在指定的时间开机才行,而不是anacron那样能够"补上"漏掉的任务。

没有评论:

发表评论