linux下删除指定crontab定时任务

来源:互联网 发布:飞鸽软件下载 编辑:程序博客网 时间:2024/05/21 00:53

1、新建两个脚本文件用来进行测试

test1.sh

ping 114.114.114.114

test2.sh

ping 8.8.8.8 

2、通过crontab -e命令编辑crontab任务,增加内容如下:

*/1 * * * * /dd/shell/test1.sh
*/1 * * * * /dd/shell/test2.sh

添加完成后,查看下crontab内容:

[root@localhost shell]# crontab -l
*/1 * * * * /dd/shell/test1.sh
*/1 * * * * /dd/shell/test2.sh


增加了crontab任务后,在/var/spool/cron目录下会有一个当前登录账号命名的文件。比如我的登录账号是root。则会存在一个root文件。该文件的内容就是刚添加的crontab任务。

[root@localhost cron]# cat /var/spool/cron/root 
*/1 * * * * /dd/shell/test1.sh
*/1 * * * * /dd/shell/test2.sh

3、删除crontab内容里的test2.sh的任务

其实该处是使用sed命令来处理/var/spool/cron/root 文件,将含test2.sh的行的内容删除掉。

 sed -i '/test2.sh/d' /var/spool/cron/root 

命令执行完后,再通过crontab -l命令查看。

[root@localhost shell]# crontab -l
*/1 * * * * /dd/shell/test1.sh

可以看到test2.sh的任务被删除掉了。通过观察,test2.sh的脚步也不再被执行。说明确实删除成功。


4、删除crontab内容空白行

在执行完上面的sed -i的命令后,crontab -l会发现多出一行空白,如果你觉得看的别扭,你可再通过如下sed命令把空白行删除掉。

 sed -i '/^$/d' /var/spool/cron/root


0 0
原创粉丝点击