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
[1] => 0 0 * * * wget php -q
[2] => 0 0 * * * wget php -q
[3] => * * * * * wget php -q
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 " 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?
8 Answers
To add a job to crontab:
(crontab -u mobman -l ; echo "*/5 * * * * perl /home/mobman/test.pl") | crontab -u mobman -To remove a job from crontab:
crontab -u mobman -l | grep -v 'perl /home/mobman/test.pl' | crontab -u mobman -Remove everything from crontab:
crontab -r
Nothing is tricky: - is STDOUT in Linux!
From a root prompt type
crontab -eYou 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.
View Users Cronjob
Use the following syntax to view waqleh user's cronjob:
crontab -u waqleh -lView current user's Cronjob
Just type the following command:
crontab -lSpecific user's cron file
crontab -u USERNAME -lThis 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/crontabRemove all cron jobs
If and only if you want to stop all cron jobs, you can remove them entirely with:
crontab -rThis 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 -eSpecific user's cron file
crontab -u USERNAME -eeach line represent a cron job. You can remove any cron (if you are using nano by clicking ctrl+k) then save and exit
crontab -l | grep -v 'wget php -q | crontab -crontab -l lists the current crontab jobs
grep -v filter some line
crontab - adds all the printed stuff into the crontab file.
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 - 1 You can run crontab without arguments, and feed it the new crontab via stdin.
For those who like me can't get out of vi:
EDITOR=nano crontab -eIn editor you can delete/change everything what you want
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.