计划任务
计划任务主要是做一些周期性任务,目前主要用途是定期备份数据。
一次调度执行at
语法格式
at <TIMESPEC>now +5minteatime tomorrow (teatime is 16:00)noon +4 days5pm august 3 2021按ctrl+D退出任务编辑
at允许使用一套相当复杂的指定时间的方法。
1)能够接受在当天的hh:mm(小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。 例如:04:00
2)能够使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等比较模糊的词语来指定时间。
3)能够采用12小时计时制,即在时间后面加上AM(上午)或PM(下午)来说明是上午还是下午。 例如:12pm
4)能够指定命令执行的具体日期,指定格式为month day(月 日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年),指定的日期必须跟在指定时间的后面。 例如:04:00 2009-03-1
5)能够使用相对计时法。指定格式为:now + count time-units ,now就是当前时间,time-units是时间单位,这里能够是minutes(分钟)、hours(小时)、days(天)、weeks(星期)。count是时间的数量,几天,几小时。 例如:now + 5 minutes 04pm + 3 days
6)能够直接使用today(今天)、tomorrow(明天)来指定完成命令的时间。安装at服务
[root@localhost ~]# yum install at -y
例1 五分钟后创建xiaohong用户(按ctrl+D退出任务编辑)
[root@localhost ~]# systemctl start atd[root@localhost ~]# systemctl enable atd[root@localhost ~]# at now +5minat> useradd xiaohongat> <EOT>job 4 at Thu Mar 11 10:58:00 2021[root@localhost ~]# atq4 Thu Mar 11 10:58:00 2021 a root[root@localhost ~]# id xiaohongid: xiaohong: no such useruid=1003(xiaohong) gid=1003(xiaohong) 组=1003(xiaohong)[root@localhost ~]# atq[root@localhost ~]#
例2 两分钟后执行脚本at.jobs,内容是在/root目录创建以日期为名的文件
[root@localhost ~]# vim at.jobstouch /root/`date +%F`.txt[root@localhost ~]# at now +2min < at.jobsjob 6 at Thu Mar 11 11:11:00 2021[root@localhost ~]# lsanaconda-ks.cfg at.jobs test1[root@localhost ~]# ls2021-03-11.txt anaconda-ks.cfg at.jobs test1
例3 在当天11点17分,创建用户xiaowang
[root@localhost ~]# at 11:17at> useradd xiaowangat> <EOT>job 7 at Thu Mar 11 11:17:00 2021[root@localhost ~]# atq7 Thu Mar 11 11:17:00 2021 a root[root@localhost ~]# id xiaowangid: xiaowang: no such user[root@localhost ~]# date2021年 03月 11日 星期四 11:16:22 CST[root@localhost ~]# date2021年 03月 11日 星期四 11:18:14 CST[root@localhost ~]# id xiaowanguid=1004(xiaowang) gid=1004(xiaowang) 组=1004(xiaowang)[root@localhost ~]# atq[root@localhost ~]#
循环调度执行cron用户级
cron执行之前必须启动守护进程
[root@localhost ~]# systemctl start crond[root@localhost ~]# systemctl enable crond[root@localhost ~]# ps aux |grep crondroot 6381 0.0 0.0 126284 1688 ? Ss 10:11 0:00 /usr/sbin/crond -n
crond进程会每分钟处理一次计划任务
存储位置
[root@localhost ~]# ls /var/spool/cron
管理命令
[root@localhost ~]# crontab -l # 列出当前用户所有计划任务[root@localhost ~]# crontab -r # 删除当前用户计划任务[root@localhost ~]# crontab -e # 编辑当前用户计划任务管理员可以使用 -u username,去管理其他用户的计划任务[root@localhost ~]# vi /etc/cron.deny # 这个文件中加入的用户名无法使用cron
cron语法格式 ```bash 分 时 日 月 星期 命令
- 表示任何数字都符合 0 2 /run.sh # 每天的2点 0 2 14 /run.sh # 每月14号2点 0 2 14 2 /run.sh # 每年2月14号2点 0 2 5 /run.sh # 每个星期5的2点 0 2 6 5 /run.sh # 每年6月份的星期5的2点 0 2 2 5 /run.sh # 每月2号或者星期5的2点 星期和日同时存在,那么就是或的关系 0 2 2 6 5 /run.sh # 每年6月2号或者星期5的2点 /5 /run.sh # 每隔5分钟执行一次 0 2 1,4,6 /run.sh # 每月1号,4号,6号的2点 0 2 5-9 * /run.sh # 每月5-9号的2点
- /run.sh # 每分钟 0 /run.sh # 每整点
- 2 /run.sh # 每月2号的每分钟
<a name="bQTmU"></a>## 循环调度执行cron系统级系统任务执行在/etc/crontab里```bash[root@localhost ~]# vim /etc/crontab # 默认没有定义任何计划任务[root@localhost ~]# ls /etc/cron.d # 定义的计划任务每个小时会执行0hourly sysstat[root@localhost ~]# cat /etc/cron.d/0hourly# Run the hourly jobsSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root01 * * * * root run-parts /etc/cron.hourly # 每小时01分以root身份执行/etc/cron.hourly/目录下的所有脚本
- 2 /run.sh # 每月2号的每分钟
- crond仅仅会执行每小时定义的脚本,在/etc/cron.hourly
```bash
[root@localhost ~]# ls /etc/cron.hourly/
0anacron
[root@localhost ~]# 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.dailyfi 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 # anacron是用来检查是否有错过的计划任务需要被执行
```bash[root@localhost ~]# cat /etc/anacrontab# /etc/anacrontab: configuration file for anacron# See anacron(8) and anacrontab(5) for details.SHELL=/bin/shPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# the maximal random delay added to the base delay of the jobsRANDOM_DELAY=45# the jobs will be started during the following hours onlySTART_HOURS_RANGE=3-22#period in days delay in minutes job-identifier command#每天开机 5 分钟后就检查 /etc/cron.daily 目录内的文件是否被执行,如果今天没有被执行,那就执行1 5 cron.daily nice run-parts /etc/cron.daily#每隔 7 天开机后 25 分钟检查 /etc/cron.weekly 目录内的文件是否被执行,如果一周内没有被执行,就会执行7 25 cron.weekly nice run-parts /etc/cron.weekly#每隔一个月开机后 45 分钟检查 /etc/cron.monthly 目录内的文件是否被执行,如果一个月内没有被执行,就执行@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
