Run Python script forever, log errors and restart when crashes

I have a python script that continuously processes new data and writes to mongodb. In a script, its a loop whileand sleepthat runs code continuously.

What is the recommended way to run a Python script forever, registration errors when they occur, and restarting upon failure?

Could node.js foreverbe appropriate? I also run node / meteor on the same Ubuntu server.

+4
source share
4 answers

supervisord . , cron, supervisord in-process, . ps, , .

. your-program-name-stderr.log your-program-name-stdout.log, logrotate, (Debian ).

supervisord, logging python, , .

, , apt-get install supervisord root.

:

[program:myprogram]
command=/path/to/my/program/script 
directory=/path/to/my/program/base
user=myuser
autostart=true
autorestart=true
redirect_stderr=True

supervisorctl , , supervisorctl start myprogram ..

+4

- . , , -

while True:
    try:
         #functionality
    except SpecificError:
        #log exception
    except: #catch everything else
    finally:
        time.sleep(600)

init.d cron.

+1

, -, , , -, .

, Python ( Linux). python "while True", , - , , , , . , , .

, - systemd ( , Linux).

, , , .Service / ../Systemd/ /Lib/Systemd/. , :

"/ etc / systemd / system /: blocks set by the system administrator" 1

The systemd documentation is nice to read here , even if you are not an expert.

Hope this helps someone!

0
source

All Articles