0

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.

3
  • 1
    You can run a wrapper script like while true; do python attachment.py; done & Commented Nov 22, 2017 at 8:03
  • 1
    Without seeing a traceback, it's hard to say what would cause the program to exit. If you are scanning live emails, I'd wager that the production environment sees things you don't have available in your test suite on your development system. Commented Nov 22, 2017 at 8:04
  • You should try to find the cause of your exit. Simply restarting the script may not be enough for robust operation. You are likely to be failing to process the email that causes the script to exit. Commented Nov 22, 2017 at 8:13

3 Answers 3

2

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.

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

Comments

0

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...

Comments

0

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

Comments

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.