美文网首页
Linux 定时任务-crontab

Linux 定时任务-crontab

作者: Mr_戋戋 | 来源:发表于2021-01-05 11:13 被阅读0次

1.涉及到的常用命令

crontab -e        #编辑定时任务
crontab -l         #查看当前的定时任务

service crond restart        #重启 crond 服务,在编辑好定时任务后执行,定时任务可马上执行

2.编辑定时任务规范
编辑定时任务主要分成 6 项:具体代表的含义如下图:


image.png

3.实例如下(ps: 这段引用https://zhuanlan.zhihu.com/p/32662320):

实例1:每1分钟执行一次command

命令:

* * * * * command



实例2:每小时的第3和第15分钟执行

命令:

3,15 * * * * command



实例3:在上午8点到11点的第3和第15分钟执行

命令:

3,15 8-11 * * * command



实例4:每隔两天的上午8点到11点的第3和第15分钟执行

命令:

3,15 8-11 */2 * * command



实例5:每个星期一的上午8点到11点的第3和第15分钟执行

命令:

3,15 8-11 * * 1 command



实例6:每晚的21:30重启smb

命令:

30 21 * * * /etc/init.d/smb restart



实例7:每月1、10、22日的4 : 45重启smb

命令:

45 4 1,10,22 * * /etc/init.d/smb restart



实例8:每周六、周日的1 : 10重启smb

命令:

10 1 * * 6,0 /etc/init.d/smb restart



实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb

命令:

0,30 18-23 * * * /etc/init.d/smb restart

4.创建一个shell 脚本,并加入到 定时任务当中

user@VM-176-216-centos home]#mkdir www
user@VM-176-216-centos home]#cd www
user@VM-176-216-centos home]#touch f1.txt
user@VM-176-216-centos home]#vim a.sh
#!/usr/bin/bash

echo "111" >> f1.txt

user@VM-176-216-centos home]#crontab -e
* * * * * /home/www/a.sh
user@VM-176-216-centos home]#service crond restart    #ps:centos 上版本不同服务重启的命令也不相同systemctl restart crond.service

user@VM-176-216-centos home]#tail -f f1.txt

相关文章

网友评论

      本文标题:Linux 定时任务-crontab

      本文链接:https://www.haomeiwen.com/subject/heuioktx.html