Daemon vs Upstart for python script

I wrote a module in Python and want it to work continuously after startup and should stop it when I need to update other modules. I will most likely use monit to restart it if the module crashes or otherwise fails.

I went through various methods such as Daemon , Upstart and many others.

What is the best way to go so that I use this approach through all my new modules to support them forever?

+33
python daemon upstart monit python-daemon
Jul 19 '13 at 13:41
source share
3 answers

From your mention of Upstart, I will assume that this question is for a service running on an Ubuntu server.

On an Ubuntu server, a higher task is the easiest and most convenient option to always create a service that starts at the right time and can be stopped or restarted by familiar commands.

To create an upstart service, you need to add one file to /etc/init . <service-name>.conf called. An example script looks like this:

 description "My chat server" author "your@email-address.com" start on runlevel [2345] stop on runlevel [!2345] env AN_ENVIRONMENTAL_VARIABLE=i-want-to-set respawn exec /srv/applications/chat.py 

This means that every time the machine starts, it starts the chat.py program. If he dies for any reason, he will restart it. You don’t need to worry about double forcing or otherwise demonizing your code. It was an upstart for you.

If you want to stop or start your process, you can do it with

 service chat start service chat stop 

The chat name will be automatically found from the .conf file name inside /etc/init

I only cover the basics of the upstart here. There are many other possibilities to make it even more useful. Everything is available by running man upstart .

This method is much more convenient than writing your own demonization code. The 4-8 line configuration file for the Ubuntu built-in component is much less error prone than the secure double fork code and then another process monitor to make sure it does not disappear.

Monit is a little red herring. If you want downtime alerts, you still need to run the monitoring program on a separate server. Rely on the upstart so that the process always runs on the server. Then run another service that ensures that the server is actually running. Downtime occurs for various reasons. A process running on the same server will not tell you anything if the server itself goes down. You need a separate machine (or a third-party vendor such as pingdom) to warn you of this condition.

+67
Jul 24. '13 at 14:02
source share

You can check the supervisor . What it can do is start the process at system startup, and then save it until it shuts down.

The simplest configuration file:

 [program:my_script] command = /home/foo/bar/venv/bin/python /home/foo/bar/scripts/my_script.py environment = MY_ENV_VAR=FOO, MY_OTHER_ENV_VAR=BAR autostart = True autorestart = True 

Then you can associate it with /etc/supervisord/conf.d , run sudo supervisorctl to enter the supervisor management console, enter reread so that the supervisor reread new configuration entry and update to display new programs in the status list.

To start / restart / stop the program, you can run sudo supervisorctl start/restart/stop my_script .

+5
Jul 28 '13 at 10:35
source share

I used old-style initscript with the start-stop-daemon utility. Look at skel in / etc / init.d

+3
Jul 27 '13 at 17:59
source share



All Articles