简体   繁体   中英

python restart after unexpected exit with `while true` loop

l wrote a python script with while true to do task to catch email's attachment, but sometimes l found out it would exit unexpectedly on server.

l run it on my local for more than 4 hours with no problem, so l can confirm that the code is correct.

So is there a kind of mechanism to restart python when it exit unexpectedly, such as process monitoring? l am a novice in linux.

remark: l run this python script like python attachment.py & in a shell script.

While @triplee's comment will definitely do the trick, I would worry that there is something going on that you would be better-off understanding. That is, why the script is failing.

Without further details, it's difficult to speculate what might be happening. As a first debugging effort, you might try wrapping the entire body within the while True in a try ... except... block, and use the except block to log the error and/or the program state. That is,

while True:
    try:
        ... do some stuff...
    except:
        ... log the exception, print to screen, record the values of key variables, etc.
        continue

This would allow you to understand what is happening during the failure, and to write more robust code that handles that event.

You can try to use Supervisor to manage your process. The Supervisor able to configure the bevhiour of the process exit status and try to restart it.

Attached is the official document and the example in Ubuntu :

example configuration

[program:nodehook]
command=/usr/bin/node /srv/http.js
directory=/srv
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/webhook/nodehook.err.log
stdout_logfile=/var/log/webhook/nodehook.out.log
user=www-data
environment=SECRET_PASSPHRASE='this is secret',SECRET_TWO='another secret

l run it on my local for more than 4 hours with no problem, so l can confirm that the code is correct.

You could be surprised by the number of bugs that only reveals after months if not years of correct processing... What you confirm is that the code does not break on first action, but unless you have tested it with all possible corner cases in input (including badly formatted ones) you cannot confirm that it will never break.

That is the reason why a program that is intented to run unattendedly should be carefully designed to always (try to * ) leave a trace before exiting. try: except: and the logging module are your best friends here.


* Of cause in case of a system crash or a power outage there's nothing you can do at user program level...

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