How to write a cron job that clears all cronjobs


I have a cronjob that occasionally sticks (ie the php file just keeps running indefinately and stops more cronjobs occuring).

I’ve been told that it’s possible to write a cron job that clears all cronjobs currently running - please could someone tell me how to do this?

Many thanks,


The question to address is why your script hangs, and adding some error control to prevent it from hanging in the first place.

the crontab command itself doesn’t have an option to kill running cron jobs. There is probably a way to find running jobs for the user and kill them. Or write some kind of marker to a temp file when the cron starts and remove it as it is ending, so that you can have another cron check to see if it hung and kill it.

The much cleaner approach is to understand why the script hangs and fix it so that it won’t.