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. The 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 for upstart + monit? If I just want to be sure the script is always running, is monit required at all?


2 Answers 2


The simplest way to do this is place this in /etc/init/something.conf:

start on runlevel [2345]
stop on runlevel [016]

exec python /path/to/your/script.py

Respawn will start it back up if it is killed or exits non-zero (like an uncaught exception). This will work going back to Ubuntu 10.04.

If you have 12.04 you can get more fancy. The above will run your script as root. In 12.04 you can add setuid/setgid:

start on runlevel [2345]
stop on runlevel [016]

setuid nobody
setgid nogroup
exec python /path/to/your/python.py

If your script exits when there is no network available and you plan to run it on an unstable network connection, well, you should fix that and just make it stay alive/retry. But if you can't, you may also need to have it manually started whenever a network device comes up. So you can place this in /etc/network/if-up.d/yourscript (make it executable with chmod +x)

exec start wait-for-state WAITER=$IFACE-yourscript WAIT_FOR=something

Where yourscript is just something arbitrary and unique to this particular script, and "something" is the same as the job name (such as the /etc/init/something.conf suggested earlier).

  • Yeah ! So this is using upstart right ? Does this mean that the script will also be started at boot (which would be great !) ? And respawn is enough so that you don't need to setup monit ?
    – sebpiq
    Aug 16, 2012 at 12:25
  • Yes, the 'start on runlevel [2345]' means at the time when the system enters "multi user mode". Long term there's a plan forming to replace the very old unixy "runlevel" with things like 'start on starting network-services' and 'stop on stopping network-services' so that it reads more logically.
    – SpamapS
    Aug 16, 2012 at 22:48
  • Awesome ! Thanks a lot that's exactly what I needed.
    – sebpiq
    Aug 17, 2012 at 7:37

The usual approach from the old days was to write a file containing a time stamp or the process id to /tmp and then check if this process id was still running or respectivly if the time stamp was still recent.

Also you could do a simple while [ 1 ]; do phyton-script.ph; done wich would restart the script in case it returns.

For more possibilities please provide more information on your script. Especially on why you expect it to exit or crash unexpectedly.

  • the script just scraps tweets from twitters using a streaming API. So basically there's an infinite loop waiting for tweets to be pushed, executing some code, and back to waiting. I just want this to be running without worrying ... if there's a bug and it crashes, and so on ...
    – sebpiq
    Aug 14, 2012 at 14:49
  • A similar answer to yours (which I accepted) : superuser.com/questions/461546/…
    – sebpiq
    Aug 14, 2012 at 15:00
  • Also a cron-job to check if the script is still running might be a good idea.
    – con-f-use
    Aug 15, 2012 at 18:03

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.