Delete the specified crontab timer task under Linux

1. Create two new script files to test

test1.sh

ping 114.114.114.114

test2.sh

ping 8.8.8.8 

2. Edit crontab task by crontab -e command, and add the following contents:

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

After adding

, check the crontab content:

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

added crontab task, in /var/spool/cron directory will have a file named the current login account. Let’s say My login is root. A root file exists. The contents of this file are the crontab tasks that you just added.

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

3. Delete test2.sh from crontab

is where sed is used to process the /var/spool/cron/root file and delete the line containing test2.sh.

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

Crontab -l command after the
command.

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

you can see that the task for test2.sh has been removed. Through observation, the steps of test2.sh are no longer performed. Indicates that the deletion was successful.

4. Delete crontab blank line

after executing the sed-i command above, crontab -l will see an extra line of white space. If you feel awkward, you can delete the blank line with the following command: sed.

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


Read More: