ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux Crontab 定时任务

2021-09-20 02:33:19  阅读:156  来源: 互联网

标签:01 log crontab Crontab Linux 定时 root smb localhost


一.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的使用
我们常用的命令如下:
crontab [-u username]    //省略用户表表示操作当前用户的crontab
1. -e (编辑工作表)
2. -l (列出工作表里的命令)
3. -r (删除工作作)
我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。
crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有
* 取值范围内的所有数字
/ 每过多少个数字
- 从X到Z
,散列数字

 

三.常见定时任务设置
实例1:每1分钟执行一次myCommand

* * * * * myCommand

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

3,15 * * * * myCommand

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

3,15 8-11 * * * myCommand

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

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

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

3,15 8-11 * * 1 myCommand

实例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

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

0 23 * * 6 /etc/init.d/smb restart

实例11:每一小时重启smb

0 */1 * * * /etc/init.d/smb restart

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

0 23-7/1 * * * /etc/init.d/smb restart

 

四.实例操作

1.文件实时写入:

1) 查看定时任务状态

[root@localhost ~]#service crond status
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 (running) 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) 关闭/开启定时任务

[root@localhost ~]# service crond stop
Redirecting to /bin/systemctl stop crond.service
[root@localhost ~]# service crond start
Redirecting to /bin/systemctl start crond.service

3) 编辑定时任务

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

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

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

5) 验证校验生成

[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) 预制数据:

[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) 添加定时任务

[root@localhost ~]# crontab -e

* * * * * find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} \;

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

[root@localhost test20210920]# du -sh *
0    file1.log
0    file2.log
0    file3.log
0    file4.log
0    file5.log

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

[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格式错误,即没有按照规则写

查看原命令并修正:

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

修改后正常保存:

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

 2.接以上错误:

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

解决方式: 

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

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

 

标签:01,log,crontab,Crontab,Linux,定时,root,smb,localhost
来源: https://www.cnblogs.com/mrwhite2020/p/15313228.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有