How to restart the Python script automatically if it is killed or dies
On Ubuntu (until 14.04, 16.04 and later use systemd) can use upstart to do so, better than a cron job. You put a config setup in /etc/init
and make sure you specify respawn
It could be a minimal file /etc/init/testing.conf
(edit as root
):
chdir /your/base/directory
exec python testing.py
respawn
And you can test with /your/base/directory/testing.py
:
from __future__ import print_function
import time
with open('/var/tmp/testing.log', 'a') as fp:
print(time.time(), 'done', file=fp)
time.sleep(3)
and start with:
sudo start testing
and follow what happens (in another window) with:
tail -f /var/tmp/testing.log
and stop with:
sudo stop testing
You can also add [start on][2]
to have the command start on boot of the system.
You could also take a more shell oriented approach. Have your cron
look for your script and relaunch it if it dies.
Create a new crontab by running
crontab -e
. This will bring up a window of your favorite text editor.Add this line to the file that just opened
*/5 * * * * pgrep -f testing.py || nohup python /home/you/scripts/testing.py > test.out
Save the file and exit the editor.
You just created a new crontab
which will be run every 5 minutes and launch your script unless it is already running. See here for a nice little tutorial on cron
. The official Ubuntu docs on cron
are here.
The actual command being run is pgrep
which searches running processes for the string given in the command line. pgrep foo
will search for a program named foo
and return its process identifier. pgrep -f
makes it search the entire command line used to launch the program and not only the program name (useful because this is a python script).
The ||
symbol means "do this if the previous command failed". So, if your script is not running, the pgrep
will fail since it will find nothing and your script will be launched.
You shouldn't really use this for production, but you could:
#!/bin/sh
while true; do
nohup python testing.py >> test.out
done &
If, for any reason, python process exits, the shell loop will continue and restart it, appending to the .out
file as desired. Nearly no overhead and takes very little time to set up.