Linux定时任务调度(Crond&&at)

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 命令执行的时候是进行反复执行

  1. at 命令是一次性定时计划任务,at的守护进程atd会以后台模式运行,检查作业队列来运行
  2. 默认情况下,atd 守护进程每60秒检查作业队列,有作业时,会检查作业运行时间,如果时间与当前时间匹配,则运行此作业
  3. at 命令是一次性定时计划任务,执行完一个任务后就不再执行该任务了
  4. 在使用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 ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值