How do I run a Python script in the background and restart it after a crash?

I have a very simple Python script that I'd like to be always running on my Ubuntu 12.04 server. I thought of using upstart + monit. The problem is that those tools seem rather complicated for a simple mortal like me, and I cannot find a simple example on the web.

Is upstart + monit overkill? Does somebody know a simpler alternative, or a good tutorial for upstart + monit? If I just want to be sure the script is always running, is monit required at all?


Solution 1:

The simplest way to do this is place this in /etc/init/something.conf:

start on runlevel [2345]
stop on runlevel [016]

respawn
exec python /path/to/your/script.py

Respawn will start it back up if it is killed or exits non-zero (like an uncaught exception). This will work going back to Ubuntu 10.04.

If you have 12.04 you can get more fancy. The above will run your script as root. In 12.04 you can add setuid/setgid:

start on runlevel [2345]
stop on runlevel [016]

respawn
setuid nobody
setgid nogroup
exec python /path/to/your/python.py

If your script exits when there is no network available and you plan to run it on an unstable network connection, well, you should fix that and just make it stay alive/retry. But if you can't, you may also need to have it manually started whenever a network device comes up. So you can place this in /etc/network/if-up.d/yourscript (make it executable with chmod +x)

#!/bin/sh
exec start wait-for-state WAITER=$IFACE-yourscript WAIT_FOR=something

Where yourscript is just something arbitrary and unique to this particular script, and "something" is the same as the job name (such as the /etc/init/something.conf suggested earlier).

Solution 2:

The usual approach from the old days was to write a file containing a time stamp or the process id to /tmp and then check if this process id was still running or respectivly if the time stamp was still recent.

Also you could do a simple while [ 1 ]; do phyton-script.ph; done wich would restart the script in case it returns.

For more possibilities please provide more information on your script. Especially on why you expect it to exit or crash unexpectedly.