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

注:计划任务中的“分”字段必须有数值,绝对不能为空或者是"*",日和星期不能他同时使用,否则发生冲突

原文地址:https://www.cnblogs.com/liujiaxin2018/p/13841364.html