我知道输入/etc/cron.d/
是自动执行的。但我也发现了这些/etc/
/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.monthly/
/etc/cron.weekly/
Run Code Online (Sandbox Code Playgroud)
在/etc/cron.d/
我发现0hourly
其中有这个内容:
01 * * * * root run-parts /etc/cron.hourly
Run Code Online (Sandbox Code Playgroud)
没有名为*daily
,*monthly
或 的文件*weekly
。
这是否意味着如果我在/etc/cron.hourly
其中添加脚本会自动执行?并且这不会发生在/etc/cron.daily
,/etc/cron.monthly/
和/etc/cron.weekly/
?
编辑:
My/etc/crontab
是空的,除了初始化变量SHELL
,PATH
和MAILTO
。
在/etc/cron.hourly/
我找到了0anacron
似乎检查cron.daily
今天是否已运行的脚本。我还发现/etc/anacron
其中包含:
# /etc/anacrontab: configuration file for 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
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#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
Run Code Online (Sandbox Code Playgroud)
我想它给了我一些阅读要做。特别是anacron(8)
和anacrontab(5)
。
CentOS 在这方面似乎和 Ubuntu 类似,只是配置略有不同。Ubuntu 使用 anacron 来运行每日/每周/每月的作业,它们在/etc/crontab
和 中配置/etc/anacrontab
。
使用 CentOS,首先我们有:
# cat /etc/cron.hourly/0anacron
#!/bin/sh
# Check whether 0anacron was run today already
if test -r /var/spool/anacron/cron.daily; then
day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi
# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power >/dev/null 2>&1
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron -s
Run Code Online (Sandbox Code Playgroud)
它每天检查/运行一次 anacron,然后:
# cat /etc/anacrontab
# /etc/anacrontab: configuration file for 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
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#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
Run Code Online (Sandbox Code Playgroud)
其中配置了每日、每周和每月的 crontab。