6

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 ?

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

2 Answers 2

10

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:

#!/bin/bash
until myscript.py; do
    echo "'myscript.py' crashed with exit code $?. Restarting..." >&2
    sleep 1
done

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.

0
1

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.

So,

#!/bin/bash

myscript(){
    python3 myscript [args..]
}

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

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!

4
  • 1
    Completely unnecessary if the python script has the proper shebang. And this shebang will indicate the proper Python version.
    – xenoid
    Commented Jun 22, 2017 at 11:49
  • And what if it doesn't? Commented Jun 23, 2017 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. Commented Jun 23, 2017 at 12:23
  • And what if it doesn't? Then you add one.
    – xenoid
    Commented Jun 23, 2017 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .