Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  16] [ 0]  / answers: 1 / hits: 26661  / 2 Years ago, tue, december 28, 2021, 2:48:08

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?


More From » python

 Answers
7

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



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

respawn
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]

respawn
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)



#!/bin/sh
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).


[#36170] Wednesday, December 29, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nsuainner

Total Points: 141
Total Questions: 102
Total Answers: 109

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
nsuainner questions
Sat, Oct 22, 22, 22:47, 2 Years ago
Sun, Oct 2, 22, 12:53, 2 Years ago
Tue, May 31, 22, 05:35, 2 Years ago
;