Linux系统定时任务
让计算机在指定的时间完成指定的工作
at一次性定时任务
- 确定系统已经安装at
[root@openmind /]# which at /usr/bin/at [root@openmind /]# at -V at version 3.1.13
- 确定at服务已经启动
service atd restart
at的访问控制

at命令

案例:
at now +2 minutes # 在两分钟之后执行hello.sh脚本 [root@openmind /]# cat hello.sh #!/bin/sh echo "hellooooooo" >> /root/hello.log [root@openmind ~]# at> /root/hello.sh >> /root/hello.log [root@openmind ~]# at now +1 minutes at> /hello.sh at># 【Ctrl + D保存退出】 job 3 at Tue Jul 27 14:03:00 2021
atq: 查看当前需要的尚未执行的任务

at -c [qid]: 查看执行任务的详情

🍎 在指定时间重启 【重启建议人工执行】
[root@openmind /]# at 16:00 2021-07-27 at> /bin/sync at> /sbin/shutdown -r now at>job 6 at Tue Jul 27 16:00:00 2021
★★★★★ 🍎crontab循环定时任务
crontab访问控制

crontab命令格式
crontab 【选项】
-e: 编辑crontab定时任务
-l: 查询crontab任务
-r: 删除当前用户所有的crontab任务
crontab编辑任务
[root@openmind /]# crontab -e # 进入crontab编辑界面,会打开vim编辑你的工作 ***** 执行的任务




crontab查看定时任务
crontab -l
删除root用户所有的定时任务
crontab -r
注意事项
1. 六个选项都不能为空 2. crontab定时任务,最小有效时间是分钟,最大时间范围是月 3. 定时任务的命令要写绝对命令
系统的crontab设置
crontab -e是每个用户执行的命令,即不同的用户身份可以执行自己的定时任务。有些定时任务需要系统执行,就需要编辑/etc/crontab这个配置文件
vim /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
执行系统的定时任务方法
- 手工执行定时任务(不推荐)
- 系统执行定时任务
- 第一种: 将需要定时执行的脚本复制到
/etc/cron.{daily,weekly,monthly}目录中的任意一个 - 第二种: 修改
/etc/crontab配置文件
- 第一种: 将需要定时执行的脚本复制到
anacron配置(定时任务的保障机制) - anacron:舔狗或许会迟到,但从不会缺席
anacron是用来保证在系统关机的时候错过的定时任务,可以在系统开机之后再执行。


cron.daily工作来说明执行的过程

学而不思则罔,思而不学则殆!

浙公网安备 33010602011771号