为什么 `/etc/crontab` 中的 `cron.hourly` 行是唯一没有 `test -x /usr/sbin/anacron` 的行?

fin*_*oot 3 linux debian cron crontab

我的还没改/etc/crontab呢 它仍然是默认内容,这也是许多 Linux 发行版的默认内容:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
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 )
#
Run Code Online (Sandbox Code Playgroud)

为什么这cron.hourly条线是唯一没有的test -x /usr/sbin/anacron?为什么也没有cron.hourlyin anacrontab


更新:

正如@JakeGould 所评论的,答案是“究竟是test -x /usr/sbin/anacron做什么的?” 例如,可以在https://unix.stackexchange.com/questions/26088/etc-crontab-what-does-test-x-stand-for找到。但主要问题仍然存在:为什么 的程序cron.hourly与其他行不同?

Aus*_*arn 5

因为Anacron设计用于工作的最小时间段是每天,所以它不能用于管理每小时的 cron 作业。

Anacron 旨在处理一个非常具体的用例,即确保不经常运行的 cron 作业仍然可以在不总是开机的系统上运行。实际上,几乎没有什么既需要每小时运行,需要在错过每小时运行的情况下立即运行的。鉴于此,Anacron 的开发人员决定允许使用更简单(也更可靠)的代码,这些代码不关注先前任务的运行时间,只关注它们运行的​​日期。反过来,这意味着 Anacron 设计为按天运行,而不是像大多数 cron 实现那样按小时或分钟运行,因此它不能用于处理小时作业。

实际上,Anacron 本身也不实际进行任何调度,这就是为什么您的那些条目在/etc/crontab那里的原因,如果没有这些条目,anacron 将无法运行(或可能仅在启动时运行),因此它们在那里以确保它即使系统长时间运行。

就一般的小时工而言,您可能会发现/etc/cron.hourly系统上是空的。大多数经典用途(或什至更频繁的 cron 作业)是您确保某些东西是最新的,或者通过轮询检查某些硬件或软件的状态的情况。使用事件驱动模型而不是轮询模型可以更好地为这两个用例提供服务,因此通常已迁移到此类设计。