Daemon vs Upstart for python script

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 15:25:45

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

On an Ubuntu server an upstart job is really the simplest and most convenient option for creating an always on service that starts up at the right time and can be stopped or reloaded with familiar commands.

To create an upstart service you need to add a single file to /etc/init. Called <service-name>.conf. 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 everytime the machine is started it will start the chat.py program. If it dies for whatever reason it will restart it. You don't have to worry about double forking or otherwise daemonizing your code. That's handled for you by upstart.

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

service chat start 
service chat stop

The name chat is automatically found from the name of the .conf file inside /etc/init

I'm only covering the basics of upstart here. There are lots of other features to make it even more useful. All available by running man upstart.

This method is much more convenient, than writing your own daemonization code. A 4-8 line config file for a built in Ubuntu component is much less error prone than making your code safely double fork and then having another process monitor it to make sure it doesn't go away.

Monit is a bit of a red herring. If you want downtime alerts you will need to run a monitoring program on a separate server anyway. Rely on upstart to keep the process always running on a server. Then have a different service that makes sure the server is actually running. Downtime happens for many different reasons. A process running on the same server will tell you precisely nothing if the server itself goes down. You need a separate machine (or a third party provider like pingdom) to alert you about that condition.

You could check out supervisor. What it is capable of is starting a process at system startup, and then keeping it alive until shutdown.

The simplest configuration file would be:

[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 could link it to /etc/supervisord/conf.d, run sudo supervisorctl to enter management console of supervisor, type in reread so that supervisor notices new config entry and update to display new programs on the status list.

To start/restart/stop a program you could execute sudo supervisorctl start/restart/stop my_script.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!