I have a very simple Python script that I'd like to be always running on my ubuntu 12.04 server. I thought of using upstart + monit. Problem is that those tools seem rather complicated for a simple mortal like me, and I cannot find a simple example on the web.

Is upstart + monit overkill ? / Does somebody know a simpler alternative ? / or a good tutorial ?

  • As I just want the script to be running, isn't upstart enough ? Monit is needed only to make a test periodically right !? – sebpiq Aug 14 '12 at 14:28
  • For reference, exact same question was asked here: askubuntu.com/q/175751/753946 – PHPirate Mar 28 at 17:36

Taken from this answer:

You could write a bash script monitor.sh which starts your python script and restarts it if it crashes but doesn't restart if the script exits normally:

until myscript.py; do
    echo "'myscript.py' crashed with exit code $?. Restarting..." >&2
    sleep 1

Then just start the monitor script in background:

nohup monitor.sh &

As an alternative to nohup, you may also consider tmux or screen for running background processes.


The above answer is great. I can't comment as of now so maybe improve the answer here.

You might even want to write a function for "myscript" as it is a python script and not running under the bash env.



    python3 myscript [args..]

until myscript; do
    echo "'myscript' crashed with exit code $?. Restarting..." >&2
    sleep 1

New people to python or bash wouldn't know this small thing. Might help them. I used to struggle with such small stuff in the beginning. Do not want any others to!

  • Completely unnecessary if the python script has the proper shebang. And this shebang will indicate the proper Python version. – xenoid Jun 22 '17 at 11:49
  • And what if it doesn't? – Neil Agarwal Jun 23 '17 at 12:21
  • I post stuff sometimes in pretext of making newbies understand if their thing doesn't work. Everyone doesn't necessarily need to know what a shebang is. – Neil Agarwal Jun 23 '17 at 12:23
  • And what if it doesn't? Then you add one. – xenoid Jun 23 '17 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.