Search code examples
pythonlinuxubuntupython-3.xrestart

How to make a program restart after an error forces it to stop?


In Python I often use try-except blocks to except certain conditions. However, unexpected errors could potentially be raised and I can't account for all of them. How would I go about restarting a Python program when it stops running inside of an environment such as Linux?


Solution

  • You can catch BaseException class. It is the basic class for all errors so you will process them all.

    If you want keep program running when smth really nasty happens, like memory leak or segmentation fault, you should write a watchdog. Watchdog is a program that checks process with specified pid running and if not, restarts it.