Python – Restart python script if it crashes

pythonscripting

I have a VPS running a simple python scripts. However, sometimes the script crashes. How can I make sure the script starts agian?

Best Answer

Run it via daemontools. It's a very simple, straightforward framework for doing exactly what you want -- it'll run the script and wait until it exits, and then start it again. It even takes care of the daemonisation, so you only have to write the script to run in the foreground, and daemontools handles all the tricky stuff. Comes with a neat stdout/stderr logging system, too, which handles the other usually-painful part of running a custom daemon.