2

I have a program written in python; my program scrapes a value from some financial website every minute and pushes that value into my DB. My program takes like 1 or maximum 1.5 seconds to do this job. I have set a cron job to call my program every minute. I need to run my program in this way every day from 9AM to 4PM. Now sometimes I may have to stop my program to kill the program at any time between 9AM to 4PM. How can I do this?

According to this link I tried ps -o pid,sess,cmd afx | grep -A20 "cron$" and I was unable to find my program in the list since it completes its work in seconds.

Referring to this I tried /etc/init.d/cron stop and pkill cron which kills all cron jobs - which I don't want. I am running this cron in Ubuntu Linux.

4

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...
  • Thanq, It makes some sense – Chiyaan Suraj May 19 '15 at 8:47
1

Don't kill the cron daemon as it does a lot of tasks which are necessary to the system. Instead, edit the crontab file and comment out the cron job related to your script. (FYI, you do not need to restart the cron daemon after editing that.)

1

The simpliest method I can think of is just to edit crontab and comment out the cronjob.

Open crontab with:

crontab -e

And comment out # the line which is your cronjob.

1

Edit your python program and let it check for a 'do not run' file first. If such file exists, exit your program i.e.

import os.path
if os.path.isfile('/tmp/disable_mypython'): exit()

If you check for a file called for instance /tmp/disable_mypython you can easily 'disable' your program using:

touch /tmp/disable_mypython

and enable it again using:

rm /tmp/disable_mypython
  • This does not need to be atomic since cron starts the process every minute. The python program does not check a file lock, but the existence of the file. Theferor, to disable the next scheduled run you can simply touch the file. – Lambert May 19 '15 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.