6

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.

7
  • Maybe you could write a script which checks every minute if your script is running and if not it restarts it? Just a quick idea. Commented May 22, 2017 at 12:05
  • @creyD Yes. How would be the best approach to do so? Should I use Python, batch or something else? If you think Python is the best approach, that will at least point me in a direction to filter my search a little bit further (since I didn't experiment much with the OS libraries in Python). Commented May 22, 2017 at 12:06
  • I guess @Gahan answered your question aswell. I would write a simple batch script for the task. You can even start this script with windows so even if your system goes down it will start again. Commented May 22, 2017 at 12:09
  • would atexit not have been the easiest option? Commented May 22, 2017 at 12:29
  • @WhatsThePoint I guess it depends how the script ended, which I don't exactly know right now. I think it might be when I'm having internet connection issues, maybe from the discord.py API, but I'm extremely unsure as my knowledge is pretty limited in Python. If the script is totally killed (as in, I open the task manager and kill it), I don't think atexit would work. Commented May 22, 2017 at 12:49

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I will look further into subprocess and popen, it seems rather simple overall.
Thank you for the edit, that's exactly what I was looking for. I was toying with stackoverflow.com/questions/325463/….
I am very happy that my example is helpful
I will let it run a couple of days and see if it improves stability or not. If it does, I will improve this to manage return codes since without any verification, if I do some syntax error in my A script, the B script will try launching A hundreds of times per second.
Also, what is the impact of running the B script from cmd.exe instead of by just launching it directly with Windows? Asking since you recommended using a terminal (I guess you meant cmd in Windows). I might want to use pythonw.exe with a scheduled task in Windows in the future instead to make it more transparent...
|
2

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

3 Comments

Would you care to elaborate on try blocks? As in, how is the program supposed to go through try blocks if it got killed? Also, I do understand the basic syntax and can implement it, but am I supposed to add try blocks to all my events and what am I supposed to check exactly? As for subprocess, I haven't read about it yet so I'll do now, thank you.
Or is the try block supposed to be in another program that is just used as a launcher and triggers an exception if the program stops?
write your code in try block if it fails to execute it went to except block at which you can also write alternate code or if you want to skip it you can just write pass in that code let assume you don't have module name XYZ and if you import it normally it gives you import error but you can write it in try block as: try: import XYZ except: pass

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.