Linux Crontab 定时任务
一.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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了