How do I run a Python script in the background and restart it after a crash?
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.
Related videos on Youtube
![Sharvin K](https://i.stack.imgur.com/DcHLh.jpg?s=256&g=1)
Sharvin K
Updated on September 18, 2022Comments
-
Sharvin K almost 2 years
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?
-
PHPirate over 5 yearsFor reference, exact same question was asked here: superuser.com/q/461546/786969
-
-
Sharvin K almost 12 yearsA similar answer to yours (which I accepted) : superuser.com/questions/461546/…
-
con-f-use almost 12 yearsAlso a cron-job to check if the script is still running might be a good idea.
-
Sharvin K almost 12 yearsYeah ! So this is using
upstart
right ? Does this mean that the script will also be started at boot (which would be great !) ? Andrespawn
is enough so that you don't need to setupmonit
? -
Sharvin K almost 12 yearsAwesome ! Thanks a lot that's exactly what I needed.