crond 任务调度
crontab 进行定时任务的设置
任务调度:是指系统在某个时间执行的特定命令或程序
任务调度分类:
- 系统工作:有些重要的工作必须周而复始的进行,例如:病毒扫描
- 个别用户工作:个别用户可能希望执行程序,例如:对mysql数据库的备份
常见的选项
- -e 编辑crontab定时任务
- -l 查询crontab任务
- -r 删除当前用户的所有contab任务
每隔一分钟将/etc/文件的信息保存到/tmp/to.txt 目录下
*/1 * * * * ls -l /etc/ > /tmp/to.txt
[ylq@rootylq ~]$ crontab -e
no crontab for ylq - using an empty one
crontab: installing new crontab
[ylq@rootylq ~]$ cd /tmp/
[ylq@rootylq tmp]$ ll
total 16
srwx------ 1 root root 0 Jan 9 09:57 mongodb-27017.sock
drwx------ 3 root root 4096 Jan 6 16:35 systemd-private-4aadeb9b48cc4d9e8fb1bb226b27fcaf-chronyd.service-whfWwq
-rw-r--r-- 1 ylq ylq 11500 Jan 10 11:11 to.txt
五个占位符说明
- 第一个* 一小时当中的第几分钟 0-59
- 第二个* 一天当中的第几小时 0-23
- 第三个* 一个月当中的第几天 1-31
- 第四个* 一年当中的第几个月 1-12
- 第五个* 一周当中的星期几 0-7(0和7都代表星期日)
crontab中特殊符号的说明
特殊符号 | 含义 |
---|---|
* | 代表任何时间 |
, | 代表不连续的时间 |
- | 代表连续时间的范围 |
*/n | 代表每隔多久执行一次 |
查看,删除crontab任务
[root@rootylq ~]# contab -l
-bash: contab: command not found
[root@rootylq ~]# crontab -l
*/1 * * * * /root/my.sh
[root@rootylq ~]# crontab -e
crontab: no changes made to crontab
[root@rootylq ~]# crontab -r
[root@rootylq ~]# crontab -e
no crontab for root - using an empty one
crontab: no changes made to crontab
at 定时任务
crond 命令执行的时候是进行反复执行
- at 命令是一次性定时计划任务,at的守护进程atd会以后台模式运行,检查作业队列来运行
- 默认情况下,atd 守护进程每60秒检查作业队列,有作业时,会检查作业运行时间,如果时间与当前时间匹配,则运行此作业
- at 命令是一次性定时计划任务,执行完一个任务后就不再执行该任务了
- 在使用at命令的时候,一定要保证atd进程启动,可以用相关指令进行查看
ps -ef|grep atd //可以检测atd是否在运行
at [选项] [时间]
Ctrl+D 结束at命令的输入
[root@rootylq ~]# clear
[root@rootylq ~]# at 5 pm+2 days
at> /bin/ls /home<EOT>
job 1 at Sat Jan 14 17:00:00 2023
[root@rootylq ~]# atq
1 Sat Jan 14 17:00:00 2023 a root
[root@rootylq ~]#
[root@rootylq ~]# atrm 2
[root@rootylq ~]# atq
1 Sat Jan 14 17:00:00 2023 a root
[root@rootylq ~]# atrm 1
[root@rootylq ~]# atq
[root@rootylq ~]#