一. 计划任务
一次性任务 at
循环任务 cron
计划任务的输出默认以邮件形式发给用户,除非重定向
二. 一次性调动at
2.1 at
两种方式使用at:
- 直接写命令
[root@localhost ~]# at now+1min
warning: commands will be executed using /bin/sh
at> mkdir /tmp/1min.txt
at> mkdir /tmp/2min.txt
at> <EOT>
job 1 at Mon Feb 8 04:11:00 2021
2.命令写文件中,然后执行文件
# at now+1min < 1.txt
2.2 atq
查询存在的at任务
[root@localhost ~]# atq
2 Mon Feb 8 04:14:00 2021 a root
三. 循环调动执行cron
crond进程每一分钟处理一次计划任务
3.1 用户级别的cron
3.1.1 用户级别的计划任务存放位置
可以直接编辑该目录下文件来编辑用户级计划任务
/var/spool/cron
3.1.2 管理方式
- 列出当前用户所有用户级计划任务
[root@DTOS cron]# crontab -l
no crontab for root
- 删除当前用户所有用户级计划任务
[root@DTOS cron]# crontab -r
- 编辑计划任务
[root@DTOS cron]# crontab -e - 管理员可以用-u来编辑其他用户的用户级计划任务
[root@localhost cron]# crontab -lu andy
[root@localhost cron]# crontab -eu andy
[root@localhost cron]# crontab -ru andy
- 禁止某些用户使用用户级cron
将其添加进/etc/cron.deny文件
[root@localhost cron]# cat /etc/cron.deny
andy
3.1.3 job格式
0 2 14 * 7 ls //每月14日的两点或每周日的两点
0 2 3-5 * * ls //每月3日到5日的两点
0 2 3,5 * * ls //每月3日和5日的两点
*/5 * * * * ls //每隔5分钟
3.2 系统级别的cron
3.2.1 配置位置一 /etc/crontab
vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
3.2.2 配置位置二 /etc/cron.d/*
/etc/cron.d/下所有文件
- 把系统级别计划任务写在/etc/cron.d目录中的文件里
- 其中这个目录下的0hourly文件比较特殊,每小时执行一次目录/etc/cron.hourly下的所有脚本
- 而目录/etc/cron.hourly下的文件0anacron很重要,他每小时一次的去启动anacrond这个程序
[root@localhost cron.d]# cat /etc/cron.hourly/0anacron
#!/bin/sh
# Check whether 0anacron was run today already
/usr/sbin/anacron -s
- 而anacrond进程的配置文件在/etc/anacrontab
里面有三行数据分别是:
4.1 每一天,延时5分钟执行一次/etc/cron.daily目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.daily 这个文件中记录的时间,如果该文件记录的时间早于一天就执行
4.2 每一周,延时25分钟执行一次/etc/cron.weekly目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.weekly 这个文件中记录的时间
4.3 每月,延时45分钟执行一次/etc/cron.monthly目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.monthly 这个文件中记录的时间
[root@localhost license]# 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