计划任务 at 和 cron 的区别:
(一)、 一次性调度执行 at
需要确保进行开启才能执行
例1(^D结束at编程): 1分钟以后添加uuuu这个用户,用atq命令查询是否还有at计划任务
例2:编辑一个任务文件,用at来调度这个文件
(二) 循环调度任务cron
crond是每分钟都会检查一次任务列表,他的频率是分钟粒度的。
crond分为用户级别和系统级别的任务
1. ============== 用户级别 =================
a) -e(编辑当前用户的crond任务),-l(列出当前用户的crond任务),-r(清空当前用户的crond任务),作为管理员,可以使用-u 管理指定用户的计划任务
b) 存储位置在 /var/spool/cron/username 下
c) 这里扩展一个知识,计划任务黑名单,/etc/cron.deny 文件中的user无法创建自己的crond任务
d) 语法格式
注意: 日 星期 是或的关系,不是与的关系
解读一些比较特殊的用法:
0 2 2 * 5 #每月2号或者周五 执行
0 2 2 6 5 # 6月2号或者周五执行
*/5 * * * * #每隔5分钟执行一次,这里不是每隔12秒执行哦。因为crond最小粒度是分钟,在*后面加/,表示 每隔多少 执行一次。
* */5 * * * #每隔5小时执行一次
0 2 1,4,6 * * #每月1 号或4号 或6号 的2点整执行
0 2 5-9 * * #每月5-9号执行
* * * * * #每分钟执行
0 * * * * #每小时0分执行
* * 2 * * #每月2号的那一天每一分钟都执行
e) 怎么查看计划任务有没有执行
1. 根据command执行结果
2. 查看crond日志: /var/log/cron
2. ============== 系统级别 =================
2.1 系统级别的计划任务通常有以下:
* 临时文件的清理 /tmp /var/tmp
* 系统信息的采集 sar
* 日志的轮转(切割) logrotate
通常不是由用户定义
2.2 系统级别的计划任务有两个定义位置:
2.3 系统级别的计划任务语法格式(查看文件 cat /etc/crontab
2.4 系统级别的计划任务定义方式
第一:可以在 /etc/crontab 中定义(添加上username),如下图;注意现在一般定义在/etc/crontab中的任务直接写在用户级别里面就可以了,不建议再写在 /etc/crontab中了;
第二: *) 可以在 /etc/cron.d/0hourly 中定义,查看0hourly这个文件,内容如下:
*) 最后一行表示,每小时01分会以root用户去执行 /etc/cron.hourly 这个目录下的脚本
[root@localhost etc]# cd cron.d
[root@localhost cron.d]# ll
total 12
-rw-r--r--. 1 root root 128 Aug 9 2019 0hourly
-rw-r--r--. 1 root root 108 Aug 6 2019 raid-check
-rw-------. 1 root root 235 Aug 9 2019 sysstat
[root@localhost cron.d]# cat 0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
*) /etc/cron.hourly 目录下默认有一个 0anacron 文件,该文件内容如下:
解析该文件: 该文件作用是启动 anacron 进程,该进程在7里面不是一个守护进程,不是一直运行,只是在每小时01分被调一次。anacron每小时被crond执行。
[root@localhost cron.hourly]# more 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
[root@localhost cron.hourly]#
*) 进入 anacron的世界
anacron执行调用的文件是: /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
~
~
job-identifier 如果是:run-parts 后面 command 跟的就是一个目录,cron.daily (每天执行的) cron.weekly(每周执行的) cron.monthly(每月执行的) period in days 是执行的频率(1就是每天,7就是隔7天),anacron会按照这个频率去执行.
delay in minutes 表示每次执行延迟多久执行
anacron每小时都会来判断任务有没有执行,如果执行过了就不会执行了,那么anacron依据什么东西去判断任务有没有执行呢?
anacron会根据这三个文件的时间戳去判断:
[root@localhost etc]# cd /var/spool/anacron/
[root@localhost anacron]# ll
total 12
-rw-------. 1 root root 9 Mar 8 10:38 cron.daily
-rw-------. 1 root root 9 Mar 2 10:29 cron.monthly
-rw-------. 1 root root 9 Mar 2 10:09 cron.weekly
[root@localhost anacron]# cat cron.daily
20200308
[root@localhost anacron]# cat cron.monthly
20200302
[root@localhost anacron]# cat cron.weekly
20200302
[root@localhost anacron]#
=========== 以下2张图片是杨老师在授课时画的:
图一: crond进程工作
图二:anacron 工作的示意图:
================================ anacron比crond更负责,它会帮你去吧错过的任务重新执行