1

I have my python script set to run from cron in Ubuntu Server. However it might take longer time to finish, before another cron event will try to start it. I would like to determine such case from script itself and if running then gracefully terminate it from python script.

Pablo
  • 28,133
  • 34
  • 125
  • 215
  • I don't know what you'd need exactly, but the `ps` command will probably be helpful. Here's a [blog post](http://andreinc.net/2010/11/07/how-to-get-the-active-process-list-on-a-linux-machine-using-python/) about someone using `subprocess` to invoke `ps` and parse its output. via a [similar question](http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python) – Conrad.Dean Mar 04 '12 at 14:41
  • 1
    Write a pid file to disk while it is running, and remove the file when it terminates. If the file exists, it's still running (or exited ungracefully). That's how many Unix utilities inform one another an instance is running. – Michael Berkowski Mar 04 '12 at 14:42
  • @Michael Is it just ordinary file? – Pablo Mar 04 '12 at 14:44
  • See http://stackoverflow.com/questions/38193/determine-if-a-ruby-script-is-already-running - Yes, just a plain text file which may even be empty. – Michael Berkowski Mar 04 '12 at 14:44

3 Answers3

1

Save your pid to a file; if the file already exists, check that the process that left its PID is still alive. (This is safer than trying to ensure you always remove the file: You can't). The full process goes like this:

Check if the checkpoint file exists. If it does not, write your PID into the file and go ahead with the computation.

If the file exists: Read the PID and check if the process is, in fact, still alive. The best way to do that is with "kill -0" (from python: os.kill), which doesn't bother the running process but fails if it does not exist. If the process is still running, exit. Otherwise, write your PID to the file etc.

There's a small chance of a race condition, but if your process is getting restarted at infrequent intervals, that should be entirely harmless: Your process could always quit in favor of a running process that exits a second later, so what does it matter if the running process manages to quit first?

alexis
  • 48,685
  • 16
  • 101
  • 161
0

There are two obvious solutions:

  1. Some kind of lock file, which it checks. If the lock file exists, then don't start, otherwise create it. (Or more aptly, in true python 'ask for forgiveness, not permission' style, try to make it and catch the error if it exists - stopping a race condition). You need to be careful to ensure this gets cleaned up when the script ends, however - even on errors, otherwise it could block future runs. Traditionally this is a .pid file which contains the process id of the running process.

  2. Use ps to check for the running process. With this solution it is harder to stop the race condition, however.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
0

One simple and effective solution is to do something like this in your crontab entry:

ps -C myscript.py > /dev/null || /path/to/myscript.py
joveha
  • 2,599
  • 2
  • 17
  • 19