https://www.douban.com/note/591925191/bash
crontab很少說,
/etc/crontab #系統的任務計劃
/etc/cron.d/ #配置文件中典型的.d文件夾
/var/spool/cron/YOUR_USER_NAME #這個文件纔是跟crontab -e/-l 關聯的
[root@localhost log]# cat /etc/cron.d/0hourly #.d文件夾裏包含了這個配置,表示每小時的01分執行
# Run the hourly jobs # /etc/cron.hourly目錄下的全部腳本
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly # run-parts是個腳本,執行某個目錄下全部的腳本。root是執行該腳本的用戶名
[root@localhost log]# cat /etc/cron.hourly/0anacron #cron.hourly目錄下的腳本,根據條件執行
#!/bin/sh # anacron命令
# 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
[root@localhost log]# cat /etc/anacrontab #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 #只在03到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
1 5 cron.daily nice run-parts /etc/cron.daily #天天都執行/etc/cront.daily/目錄下的腳本文件,真實的延遲RANDOM_DELAY+delay。這裏的延遲是5分鐘,加上上面的RANDOM_DELAY,因此實際的延遲時間是5-50之間,開始時間爲03-22點,若是機器沒關,那麼通常就是在03:05-03:50之間執行。nice命令將該進程設置爲nice=10,默認爲0,即低優先級進程。
若是RANDOM_DELAY=0,那麼表示準確延遲5min,即03:05執行cron.daily
綜上,整個邏輯流爲:
crontd進程每小時的01分執行/etc/cront.hourly/0anacron --->執行anacron -->根據/etc/anacrontab的配置執行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthlydom