Generally, stopping and starting the system cron
daemon is a bad idea. Commenting out the line isn't always convenient so here are a couple of related alternatives
Use a semaphore
One solution to this requirement is to use a semaphore - or flag - to indicate whether or not the script is permitted to run. In this instance the semaphore can be represented by the presence or absence of a file: if the file exists then do not run the script, otherwise run it. (Of course, the test could be reversed so that the script only runs if the file exists.)
Here is a sample cron
entry with the semaphore check in place:
* * * * * test ! -f /tmp/stop && /path/to/script...
To prevent the script from running, simply execute touch /tmp/stop
. To allow it to start running again, just rm /tmp/stop
.
To reverse the test, use this test for the file /tmp/run
:
* * * * * test -f /tmp/run && /path/to/script...
You would probably want to use a different semaphore file in practice to ensure that only authorised people could flag the script to stop (or start). One option might be "$HOME"/.scriptname.stop
for a script called scriptname
.
Prevent the code running
If the script is executable, and called by /path/to/script...
then simply removing executable permissions or moving it out of the way will stop the script running:
chmod a-x /path/to/script
or
mv /path/to/script{,.stop} # i.e. mv /path/to/script /path/to/script.stop
This is a quick and ugly fix that will cause cron
to generate an error each time it attempts to run your code. Normally, such errors are caught and emailed to the cron
job owner, so a little test before executing the script will prevent this situation being flagged:
* * * * * test -x /path/to/script && /path/to/script...