简体   繁体   中英

How do I automatically rerun a python app on error?

I have a Telegram bot based on free pythonanywhere servers. So, what's the problem? For some reason the execution is stopped from time to time and no errors are displayed in the console. Example .

I need my bot to be active all the time. What do I do?

On a very general level you can restart your app like this in the same file or from another file

from your_app import main

while True:
   try:
      main() # assuming this has a blocking loop as well
   except Exception as e:
      print(e) # or log it in some way
   print("Restarting main")

Even if main terminates without an error it will restart. You might need to do some clean up beforehand.

More specific to your problem:
There always could be an option that an error is silently suppressed and your code ends or something with the server, which will not show up on the console.

1)Create a batch file and run your Main.py file 2)Schedule it with the windows task scheduler with created batch file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM