I create and update telegram bots by aiogram. And sometimes when I have errors in code, they trigger a Python exception. But this exception doesn't stop the script, just stops the work of the bot.
If I have an exception, the bot stops working, but the script of the bot is still running, and my Bash script will not restart the bot. And all users need to somehow notify me and wait while I manually restart the bot script.
Is it possible to do quit() on any exception in my script? How?
And it would be even better if it were possible to write the same exception to the file at the same time.
To quit on any exception and log it, wrap your bot's main loop in a try-except, use
sys.exit(1)to exit, andtraceback.format_exc()to write the exception to a file.