守护进程 vs 暴发户 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17747605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:04:43  来源:igfitidea点击:

Daemon vs Upstart for python script

pythondaemonupstartmonitpython-daemon

提问by mtariq

I have written a module in Python and want it to run continuously once started and need to stop it when I need to update other modules. I will likely be using monit to restart it, if module has crashed or is otherwise not running.

我用 Python 编写了一个模块,并希望它在启动后连续运行,并且在需要更新其他模块时需要停止它。如果模块崩溃或未运行,我可能会使用 monit 重新启动它。

I was going through different techniques like Daemon, Upstartand many others.

我正在使用不同的技术,如DaemonUpstart和许多其他技术。

Which is the best way to go so that I use that approach through out my all new modules to keep running them forever?

哪种方法可以让我在所有新模块中使用这种方法来永远运行它们?

采纳答案by aychedee

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

从你提到的 Upstart 我会假设这个问题是针对在 Ubuntu 服务器上运行的服务。

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.

在 Ubuntu 服务器上,新贵工作实际上是创建始终在线的服务的最简单和最方便的选择,该服务在正确的时间启动并且可以使用熟悉的命令停止或重新加载。

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:

要创建新贵服务,您需要将单个文件添加到/etc/init. 称为<service-name>.conf。示例脚本如下所示:

description "My chat server"
author "[email protected]"

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.pyprogram. 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.

这意味着每次启动机器时,它都会启动chat.py程序。如果它因任何原因死亡,它将重新启动它。您不必担心双分叉或以其他方式守护您的代码。这由暴发户为您处理。

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

如果你想停止或开始你的过程,你可以这样做

service chat start 
service chat stop

The name chatis automatically found from the name of the .conffile inside /etc/init

名字chat是从.conf里面的文件名自动找到的/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.

我在这里只介绍新贵的基础知识。还有许多其他功能可以使其更加有用。运行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.

这种方法比编写自己的守护程序代码方便得多。一个 4-8 行的 Ubuntu 内置组件配置文件比让你的代码安全地双分叉然后让另一个进程监视它以确保它不会消失更不容易出错。

Monit is a bit of a red herring. If you want downtime alerts you will need to run a monitoring program on a separateserver 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.

Monit 有点像红鲱鱼。如果您想要停机警报,则无论如何都需要在单独的服务器上运行监控程序。依靠 upstart 使进程始终在服务器上运行。然后使用不同的服务来确保服务器实际运行。停机的发生有许多不同的原因。如果服务器本身出现故障,运行在同一台服务器上的进程将不会告诉您任何信息。您需要一台单独的机器(或第三方提供商,如 pingdom)来提醒您有关该情况的信息。

回答by eri

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

我使用带有 start-stop-daemon 实用程序的旧式 initscript。查看 /etc/init.d 中的 skel

回答by Maciej Gol

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

你可以看看supervisor。它的功能是在系统启动时启动一个进程,然后使其保持活动状态直到关闭。

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 supervisorctlto enter management console of supervisor, type in rereadso that supervisor notices new config entry and updateto display new programs on the statuslist.

然后你可以把它链接到/etc/supervisord/conf.d,运行sudo supervisorctl进入supervisor的管理控制台,输入reread以便supervisor注意到新的配置条目并updatestatus列表中显示新程序。

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

要启动/重新启动/停止程序,您可以执行sudo supervisorctl start/restart/stop my_script.