Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically restart a Python program if it's killed

I run a Python Discord bot. I import some modules and have some events. Now and then, it seems like the script gets killed for some unknown reason. Maybe because of an error/exception or some connection issue maybe? I'm no Python expert but I managed to get my bot working pretty well, I just don't exactly understand how it works under the hood (since the program does nothing besides waiting for events). Either way, I'd like it to restart automatically after it stops.

I use Windows 10 and just start my program either by double-clicking on it or through pythonw.exe if I don't want the window. What would be the best approach to verify if my program is still running (it doesn't have to be instant, the verification could be done every X minutes)? I thought of using a batch file or another Python script but I have no idea how to do such thing.

Thanks for your help.

like image 239
dan Avatar asked May 22 '17 12:05

dan


People also ask

How do you automatically restart a program in Python?

Instead, to restart a process in Python, you must create a new instance of the process with the same configuration and then call the start() function.

How do I restart a Python script?

How do I restart Python shell? To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.

What happens if you delete a Python script while it is running?

It happens nothing. Once the script is loaded in memory and running it will keep like this. An "auto-reloading" feature can be implemented anyway in your code, like Flask and other frameworks does.


2 Answers

You can write another python code (B) to call your original python code (A) using Popen from subprocess. In python code (B), ask the program to wait for your python code (A). If 'A' exits with an error code, recall it from B.

I provide an example for python_code_B.py

import subprocess

filename = 'my_python_code_A.py'
while True:
    """However, you should be careful with the '.wait()'"""
    p = subprocess.Popen('python '+filename, shell=True).wait()

    """#if your there is an error from running 'my_python_code_A.py', 
    the while loop will be repeated, 
    otherwise the program will break from the loop"""
    if p != 0:
        continue
    else:
        break

This will generally work well on Unix / Windows systems. Tested on Win7/10 with latest code update.

Also, please run python_code_B.py from a 'real terminal' which means running from a command prompt or terminal, and not in IDLE.

like image 81
emmanuelsa Avatar answered Sep 28 '22 08:09

emmanuelsa


for problem you stated i prefer to use python subprocess call to rerun python script or use try blocks. This might be helpful to you. check this sample try block code:

try:
  import xyz # consider it is not exist or any error code
except:
  pass # go to next line of code to execute
like image 28
Gahan Avatar answered Sep 28 '22 10:09

Gahan