计划任务
计划任务:在开机的情况下,按照计划完成的任务.
一次性:只执行一次
周期性:有规律的执行
计划任务中的输出不会输出到终端,而是通过邮件通知
一次性计划任务
at
at:用于定制一次性计划任务
语法: at 时间
时间格式
HH:MM 默认为今天,如果这个时间已经过了,则为明天
HH:MM YY-I-DD指定哪一天什么时间,日期还可以写成 MADDYY,MM/DD/YY,DD.MM.YYnow
+ 2minutes 2分钟之后﹐支持minutes, hours, days,or weeks这些单位
atq:用于查询一次性计划任务等价于at -l,看不到任务详情.需要查看可用at -c
atrm:删除一次性计划任务等价于at -d ,后面接任务编号来删除相应的计划任务
案例:
1、两分钟之后创建普通文件a.txt
# at + 2minutes
at> touch a.txt
at> <EOT> //ctrl + d 结束编辑
job 5 at Thu May 19 16:36:00 2022
周期性计划任务
#计划任务存储的位置
[root@JX01 ~]# ls /var/spool/cron/
root jack alice
#管理计划任务的命令
crontab:
-l Displays the current crontab on standard output.
-r Removes the current crontab.
-e Edits the current crontab using the editor specified.
#计划任务书写的格式
.---------------- 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
| | | | |
* * * * * command
#计划任务案例
00 02 * * * ls //每天2:00整
00 02 1 * * ls //每月1号2:00整
00 02 14 2 * ls //每年2月14号2:00整
00 02 * * 7 ls //每周日2:00整
00 02 * 6 5 ls //每年6月的周五2:00整(特殊)
00 02 14 * 7 ls //每月14号2:00整 或者 每周日2:00整,这两个时间都执行
00 02 14 2 7 ls //每年2月14号2:00整 或者 每周日2:00整,这两个时间都执行
00 02 * * * ls //每天2:00整
* 02 * * * ls //每天2:00中的每一分钟
* * * * * ls //每分钟执行ls
* * 14 2 * ls //2月14号的每分钟 1440分钟
*/5 * * * * ls //每隔5分钟
00 02 1,5,8 * * ls //每月1,5,8号的2:00整
00 02 1-8 * * ls //每月1到8号的2:00整
00 02 * 1-10 * ls
#测试计划任务的执行效果
1 编写执行脚本.
vim /crontab.sh
touch /root/`date +%F-%X`.txt
2 编排任务计划
[root@localhost ~]# crontab -e
* * 1 1 * bash /crontab.sh
3 修改日期时间为1月2日3点4分
date 01020304
修改时间为1点2分3秒
date -s 01:02:03
4 监控当前目录
watch -n 0.5 'ls /root/*.txt'
5 测试目标
* * * * 1 //每周1 每分钟会执行
* * * 1 * //1月每日 每分钟会执行
* * * 1 1 //1月的周1 每分钟会执行
* * 1 * * //每月1日 每分钟会执行
* * 1 * 1 //每月1日和每月周1 每分钟会执行
* * 1 1 * //1月1日 每分钟会执行
* * 1 1 1 //1月1日和1月的周1 每分钟都会执行