1

I have made a python script, that needs to constantly be doing some work, whenever the machine is running the script needs to be running, the problem is that sometimes it crashes from unpreventable reasons. In these situations I want it to restart.

I thought of making it a daemon, but I am not sure if it is an overkill or the correct thing to do.

3
  • Better practice would be imo to make it output what went wrong. How to do that depends on the script, and what it should do. If you run it for some time in a terminal until it crashes should give you at least a hunch. Jun 9, 2017 at 11:11
  • If the program needs to run at all times when the system is up and running you should look into the configuration of a (systemd) service that manages the program instance. There is an option to restart a service process whenever it terminates as long as the services itself isn't stopped by a pre-defined event or user intervention. Jun 9, 2017 at 11:26
  • @JacobVlijm I already do exception handling, but the problem is that sometimes the server restarts on its own or other things like memory overloads by other programs that I have no control over. Jun 9, 2017 at 11:39

2 Answers 2

1
(while true; do echo "$(date) hello world"; sleep 1; done) &

Or for your script:

(while true; do ./python_script.py; done) &
0

You can use monit tool. it's very small and useful for any type of process management and monitoring.

After Downloading binary package from this link, you can extract it to a folder on your system and then copy two files from the package to your system to install it:

cd /path/to/monit/folder
cp ./bin/monit /usr/sbin/monit
cp ./conf/monitrc /etc/monitrc  

Now edit /etc/monitrc base on your needs(reference doc). then create a init control file to enable monit on startup. now start monit like this:

initctl reload-configuration
start monit
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.