I have a Python 2.7 program running on Ubuntu16. This program is designed to work for months in the background capturing tweets. The problem is that every week or so, an error appears in one of the libraries and the program stops working without warning.
I need a solution that will re-launch the python program as soon as it crashes and exits. Does anyone have a solution? Thank you
First of all, if the script ends with an exception that you can catch, the normal thing is that you handle said exception so that the process itself manages itself and when the failure occurs, it re-initializes everything that is necessary to be able to continue.
Python allows great handling of exceptions, if you know in advance that an exception can occur that is not in your hands to correct, instead of letting your process terminate abruptly, it is better to catch said exception and terminate the work in a controlled manner you are doing (or restart it) carrying out the process safely (closing files or open sockets, controlled termination of threads, etc).
A very simplified example to illustrate what I mean is the following:
If during the execution of
main()
a type exception occursRuntimeError
(in this case it always occurs within 10 seconds of being called) it is called again repeating the process indefinitely. If it ends properly or because of another exception (eg Ctrl+ c) the process ends.If this is not possible, you have several options:
Use the Systemd daemon manager - if I remember correctly since 15.04 it is built into the system. In addition to allowing you to initialize the service manually and restart it automatically in case of failure, you can make it start at system startup. The basic idea is to create a new .service file:
The content of the basic file can be:
Edited the .service we save it and reload:
With this we can start our service whenever we want with:
Stop it with:
Enable it to be executed at system boot:
Disable it to be executed at system boot:
Get service information:
This is just a basic example, systemd has much more potential, in the previous link you have the documentation.
Use
cron
to schedule a task that checks every x time if the process is still running and if not, relaunches it.Use a second Python or bash script to launch the thread and launch it again if it doesn't finish as expected. The basic idea would be something like this:
Surely there are many more ideas, like the one @Gytree comments, using a script instead of cron that checks if the process is running every x time. However, in my opinion, the first thing is to make the script self-handle its exceptions (instead of ending abruptly when it occurs) and for everything else (for example, the process ends by unexpected shutdown of the computer) Systemd is a tool very good.