一、一次性调度执行:at
1、示例:
5分钟后:[root@localhost tengine-2.2.0]# at now +5min
四天后的中午:[root@localhost tengine-2.2.0]# at noon +4 days
在2021.2.1的4点[root@localhost tengine-2.2.0]# at 4;00 2021-2-1
2、例子
1)设置一个定时创建任务useradd uuuu和touch uuuu
2)chtl + D退出
[root@localhost ~]# at now +2min
at> useradd uuuu
at> touch uuuu<EOT>
job 2 at Mon Mar 29 11:42:00 2021
[root@localhost ~]#
3)查询任务
[root@localhost ~]# atq
2 Mon Mar 29 11:42:00 2021 a root
[root@localhost ~]#
4)验证
[root@localhost ~]# id uuuu
uid=1005(uuuu) gid=1007(uuuu) 组=1007(uuuu)
[root@localhost ~]# ls | grep uuuu
uuuu
[root@localhost ~]#
5)报错
二、循环调度执行crontab
# .---------------- 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
1、创建计划:[root@localhost ~]# crontab -e
2、查询计划:[root@localhost ~]# crontab -l
3、删除计划:[root@localhost ~]# crontab -r
4、查看任务:[root@localhost ~]# ls /var/spool/cron/
5、示例:每分钟创建一次文件
1)准备脚本:
[root@localhost ~]# vim /root/1.sh
[root@localhost ~]# cat /root/1.sh
/usr/bin/touch /root/`date +%F-%H-%M-%S`.txt
[root@localhost ~]# chmod +x /root/1.sh
2)编写任务书:
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]#
【注】任务写:* * * * * /root/1.sh**
3)查看结果:
[root@localhost ~]# ls -l /root | grep *.txt
-rw-r--r--. 1 root root 0 3月 29 15:09 2021-03-29-15-09-01.txt
[root@localhost ~]#