cron和anacrontab

https://www.douban.com/note/591925191/


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.monthly



本文转自 pk2008 51CTO博客,原文链接:
http://blog.51cto.com/837244/1981982

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值