linux系统中crontab命令、计划任务服务
1、crontab命令用于创建系统能够周期性、有规律地执行某些具体的任务
直接测试:crontab -e 进行创建、编辑任务
[root@linuxprobe test]# ls ## 查看测试文件
a.txt
[root@linuxprobe test]# ll -h
total 6.6M
-rw-r--r--. 1 root root 6.6M Oct 19 16:19 a.txt
[root@linuxprobe test]# crontab -e ## crontab -e 命令用于创建、编辑计划任务,在终端输入crontab -e命令后直接进入编辑模式,编辑之后进行保存,方法同vim编辑器使用
crontab: installing new crontab
[root@linuxprobe test]# crontab -l ## 列出计划任务服务列表,50表示分,16表示小时,向后的*依次是日、月(缺失的情况下用*表示)、1-7表示星期一到星期日。必须是绝对路径
50 16 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
## 上句命令的顺序是"分、时、日、月、星期 命令",时间没有设定时,需要用"*"进行站位。 上句命令表示每天的(周一至周日)的16:50对a.txt进行打包压缩
[root@linuxprobe test]# date
Mon Oct 19 16:48:30 CST 2020
[root@linuxprobe test]# crontab -l
50 16 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
[root@linuxprobe test]# date
Mon Oct 19 16:49:14 CST 2020
[root@linuxprobe test]# date
Mon Oct 19 16:50:39 CST 2020
You have new mail in /var/spool/mail/root
[root@linuxprobe test]# ls ## 已经执行计划任务中的打包压缩
a.tar.gz a.txt
2、cronttab -l 列出计划任务列表
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# crontab -e
crontab: installing new crontab
[root@linuxprobe test]# crontab -l ## 列出任务
5 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
[root@linuxprobe test]# crontab -e
crontab: installing new crontab
[root@linuxprobe test]# crontab -l ## 列出任务
5 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
10 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
3、crontab -r 删除计划任务
[root@linuxprobe test]# ls
a.tar.gz a.txt
[root@linuxprobe test]# crontab -l
5 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
10 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
[root@linuxprobe test]# crontab -r ## 删除计划任务服务
[root@linuxprobe test]# crontab -l
no crontab for root
4、在执行多条计划任务时,应每行一条
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# crontab -l
no crontab for root
[root@linuxprobe test]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@linuxprobe test]# crontab -l ##列出任务
10 17 * * 1-7 /usr/bin/tar -czvf /home/test/a.tar.gz /home/test/a.txt
11 17 * * 1-7 /usr/bin/rm -f /home/test/a.tar.gz
[root@linuxprobe test]# date
Mon Oct 19 17:10:01 CST 2020
[root@linuxprobe test]# ls ## 已执行计划任务1
a.tar.gz a.txt
[root@linuxprobe test]# date
Mon Oct 19 17:10:07 CST 2020
[root@linuxprobe test]# date
Mon Oct 19 17:10:38 CST 2020
[root@linuxprobe test]# date
Mon Oct 19 17:11:14 CST 2020
You have new mail in /var/spool/mail/root
[root@linuxprobe test]# ls ## 已执行计划任务2
a.txt
注:计划任务中的“分”字段必须有数值,绝对不能为空或者是"*",日和星期不能他同时使用,否则发生冲突
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律