95

I have created cron jobs for my site which is listed below and they are working fine. I print all cron job by using this PHP script:

$cronfiles=exec('crontab -l',$output);
echo "<pre>";
print_r($output);

Which outputs:

[0] => 0 0 * * * wget php -q http://www.example.com/report_send.php
[1] => 0 0 * * * wget php -q http://www.example.com/event_reminder.php
[2] => 0 0 * * * wget php -q http://www.example.com/user_reminder.php
[3] => * * * * * wget php -q http://www.example.com/cleardata.php

Now I want to delete or remove a single cron job from my server through command. For example I want to remove cron job "0 0 * * * wget php -q http://www.example.com/event_reminder.php" from server.

I tried crontab -r command which removes all cron job from my server but i want to remove specific cron job.

Can you please help me for solution?

7ochem
  • 191
anumavu
  • 951
  • 1
  • 7
  • 4

8 Answers8

132
  1. To add a job to crontab:

    (crontab -u mobman -l ; echo "*/5 * * * * perl /home/mobman/test.pl") | crontab -u mobman -
    
  2. To remove a job from crontab:

    crontab -u mobman -l | grep -v 'perl /home/mobman/test.pl'  | crontab -u mobman -
    
  3. Remove everything from crontab:

    crontab -r
    

Nothing is tricky: - is STDOUT in Linux!

Fabby
  • 34,259
Greg Hanis
  • 1,321
  • 1
    Greg: Welcome to Ask Ubuntu! ;-) Could you please review my edits and also review the editing help to improve the readability of your answers in the future... ;-) – Fabby Jan 11 '16 at 23:04
  • 6
    Please replace 1. To add a job to crontab with (crontab -u mobman -l 2>/dev/null; echo "/5 * * * perl /home/mobman/test.pl") | crontab -u mobman - (ref: http://stackoverflow.com/questions/4880290/how-do-i-create-a-crontab-through-a-script) – Yasiru G Jul 25 '16 at 09:31
  • 3
    @YasiruG I think crontab -l 2 > /dev/null is only necessary if you don't want to see the message no crontab for <user> if there is no existing cron job. By the way, we don't need the option -u mobman if we want to add the cron job for the current user. – baptx Nov 26 '17 at 23:13
  • hi, how if i only add if cron not registered/not exist before? – Jazuly Jul 27 '20 at 09:47
  • 2
    - has no special (syntactic) meaning: it's just a string as, for instance, "a". It's some sort of convention, though, that some programs interpret - as "read from stdin". (The actual stdin is usually /dev/stdin). – Luis Lavaire Aug 16 '20 at 20:08
47

From a root prompt type

crontab -e

You can now edit the file and remove the line you want remove. You can also use this to edit crontab for users if you have the prompt for that user.

By the way: I prefer to add cronjobs to /etc/crontab. Seems a bit more flexible to me.

Rinzwind
  • 299,756
17

View Users Cronjob

Use the following syntax to view waqleh user's cronjob:

crontab -u waqleh -l

View current user's Cronjob

Just type the following command:

crontab -l

Specific user's cron file

crontab -u USERNAME -l

This should list the contents of the crontab script.

View /etc/crontab

A cronjob can be also run from /etc/crontab file. To view it, enter:

less /etc/crontab

Remove all cron jobs

If and only if you want to stop all cron jobs, you can remove them entirely with:

crontab -r

This removes the entire crontab file for current user so be careful if you've got other cron jobs listed in there!

Add/Edit/Delete cron job(s)

Your user's cron file

crontab -e

Specific user's cron file

crontab -u USERNAME -e

each line represent a cron job. You can remove any cron (if you are using nano by clicking ctrl+k) then save and exit

Waqleh
  • 871
7
crontab -l | grep -v 'wget php -q http://www.example.com/event_reminder.php' | crontab -

crontab -l lists the current crontab jobs

grep -v filter some line

crontab - adds all the printed stuff into the crontab file.

Diego D
  • 171
  • This explains in detail the piped bash commands given by @rinzwind above. basically filter out what you want to remove using grep and pass all that to crontab at the command line again. The '\n' characters will separate cron job entries. The | command puts the output of the previous program into stdout by default, and this is represented as '-' as the input for the last crontab command. – Pablo Adames Oct 25 '22 at 17:44
3

To comment out the cron job at say line 2, use this command in your shell:

crontab -l | sed '2 s/^/#/g' | crontab -

Replace the number 2 by the line number of your choice, or remove it altogether to comment out all the jobs.

It can be programmatically called via a cron job itself, for instance to comment all jobs at 12:00, add this line to your crontab:

0 12 * * * crontab -l | sed 's/^/#/g' | crontab -
Eric
  • 131
0

For those who like me can't get out of vi:

EDITOR=nano crontab -e

In editor you can delete/change everything what you want

demon101
  • 159
0

Just use pipes as follows

crontab -l | grep -v 'your_job' | head -1 | crontab -

For example

crontab -l | grep -v '/usr/local/bin/my_script.sh' | head -1 | crontab -
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Apr 02 '23 at 07:26
-2

The best way to go about removing individual crontab jobs is simply to go into the script and comment out the line of script that performs the operation.