详解Linux定时任务Crontab的介绍与使用

吾爱主题 阅读:119 2024-04-05 15:06:52 评论:0

一.cron介绍

linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题。

1. var/spool/cron/

目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名

2. _/etc/crontab

这个文件负责调度各种管理和维护任务。

3. /etc/cron.d/

这个目录用来存放任何要执行的crontab文件或脚本。

我们还可以把脚本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录中,让它每小时/天/星期、月执行一次。

二.crontab的使用

我们常用的命令如下:

?
1 crontab [-u username]     // 省略用户表表示操作当前用户的 crontab

1. -e (编辑工作表)

2. -l (列出工作表里的命令)

3. -r (删除工作作)

我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。

crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有

  • * 取值范围内的所有数字
  • / 每过多少个数字
  • - 从X到Z
  • ,散列数字

三.常见定时任务设置

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

?
1 <strong>* * * * * myCommand< /strong >

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

?
1 <strong>3,15 * * * * myCommand< /strong >

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

?
1 <strong>3,15 8-11 * * * myCommand< /strong >

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

?
1 <strong>3,15 8-11 * /2 * * myCommand< /strong >

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

?
1 <strong>3,15 8-11 * * 1 myCommand< /strong >

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

?
1 <strong>30 21 * * * /etc/init .d /smb restart< /strong >

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

?
1 <strong>45 4 1,10,22 * * /etc/init .d /smb restart< /strong >

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

?
1 <strong>10 1 * * 6,0 /etc/init .d /smb restart< /strong >

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

?
1 <strong>0,30 18-23 * * * /etc/init .d /smb restart< /strong >

实例10:每星期六的晚上11 : 00 pm重启smb

?
1 <strong>0 23 * * 6 /etc/init .d /smb restart< /strong >

实例11:每一小时重启smb

?
1 <strong>0 * /1 * * * /etc/init .d /smb restart< /strong >

实例12:晚上11点到早上7点之间,每隔一小时重启smb

?
1 <strong>0 23-7 /1 * * * /etc/init .d /smb restart< /strong >

四.实例操作

1.文件实时写入

1) 查看定时任务状态

?
1 2 3 4 5 6 7 8 [root@localhost ~] #<strong>service crond status</strong> Redirecting to /bin/systemctl status crond.service ● crond.service - Command Scheduler     Loaded: loaded ( /usr/lib/systemd/system/crond .service; enabled; vendor preset: enabled)     Active: active <strong>(running)< /strong > since Mon 2021-09-20 01:22:18 CST; 24s ago   Main PID: 17516 (crond)     CGroup: /system .slice /crond .service             └─17516 /usr/sbin/crond -n

2) 关闭/开启定时任务

?
1 2 3 4 [root@localhost ~] # <strong>service crond stop</strong> Redirecting to /bin/systemctl stop crond.service [root@localhost ~] # <strong>service crond start</strong> Redirecting to /bin/systemctl start crond.service

3) 编辑定时任务

?
1 2 [root@localhost ~] # crontab -e * * * * * echo ` date '+\%Y-\%m-\%d \%H:\%M:\%S' ` >> 123.txt #每分钟执行一次

4) 查看已存在的定时任务

?
1 2 [root@localhost ~] # crontab -l * * * * * echo ` date '+\%Y-\%m-\%d \%H:\%M:\%S' ` >> 123.txt

5) 验证校验生成

?
1 2 3 4 [root@localhost ~] # cat 123.txt 2021-09-20 01:13:01 2021-09-20 01:14:01 2021-09-20 01:15:01

2. 定期清理对应目录下的文件

1) 预制数据:

?
1 2 3 4 5 6 7 8 [root@localhost test20210920] # echo abcdefg | tee -a file{1..5}.log abcdefg [root@localhost test20210920] # du -sh * 4.0K    file1.log 4.0K    file2.log 4.0K    file3.log 4.0K    file4.log 4.0K    file5.log

2) 添加定时任务

?
1 2 3 [root@localhost ~] # crontab -e   * * * * * find  /root/test20210920  - type f -name '*.log' - exec cp /dev/null {} \;

3) 检查定时任务执行,1分钟左右

?
1 2 3 4 5 6 [root@localhost test20210920] # du -sh * 0    file1.log 0    file2.log 0    file3.log 0    file4.log 0    file5.log

4) 查看定时任务执行情况:

?
1 2 3 4 5 6 [root@localhost test20210920] # tail -5 /var/log/cron Sep 20 01:56:33 localhost crontab [17797]: (root) END EDIT (root) Sep 20 01:56:51 localhost crontab [17802]: (root) BEGIN EDIT (root) Sep 20 01:56:54 localhost crontab [17802]: (root) END EDIT (root) Sep 20 01:57:01 localhost CROND[17809]: (root) CMD ( find  /root/test20210920  - type f -name '*.log' - exec cp /dev/null {} \;) Sep 20 01:58:01 localhost CROND[17819]: (root) CMD ( find  /root/test20210920  - type f -name '*.log' - exec cp /dev/null {} \;)

五.常见错误

1.errors in crontab file, can't install

解决方式:

因为你的crontab格式错误,即没有按照规则写

查看原命令并修正:

?
1 echo ` date + "%Y-%m-%d %H:%M" ` >> 123.txt

修改后正常保存:

?
1 * * * * * echo ` date + "%Y-%m-%d %H:%M" ` >> 123.txt

2.接以上错误:

邮件内报错:cat /var/spool/mail/root

解决方式: 

crontab内%需要转义,修复定时任务正常

?
1 * * * * * echo ` date '+\%Y-\%m-\%d \%H:\%M:\%S' ` >> 123.txt

以上就是详解Linux定时任务Crontab的介绍与使用的详细内容,更多关于Linux定时任务Crontab的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/mrwhite2020/p/15313228.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容