守护程序vs Upstart for python脚本 [英] Daemon vs Upstart for python script

查看:92
本文介绍了守护程序vs Upstart for python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在经历不同的技术,例如 Daemon Upstart 和许多其他

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

这是最好的选择

推荐答案

从您对Upstart的提及中,我会假设我一直在使用所有新模块中使用的方法?这个问题是针对在Ubuntu服务器上运行的服务。

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

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

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

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

此表示每次启动计算机时都会启动 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

名称 / etc / init 中的 .conf 文件的名称中自动找到chat

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

在这里我只介绍暴发户的基础知识。还有许多其他功能使其变得更加有用。通过运行 man upstart 可以全部使用。

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.

此方法比编写您自己的守护程序代码方便得多。与使您的代码安全地进行两次分叉,然后由另一个进程进行监视以确保它不会消失相比,内置的Ubuntu组件的4-8行配置文件易于出错。

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有点像鲱鱼。如果您希望获得停机警报,则无论如何都需要在单独服务器上运行监视程序。依靠新贵使进程始终在服务器上运行。然后使用另一项服务,以确保服务器实际上正在运行。造成停机的原因有很多。如果服务器本身发生故障,则在同一服务器上运行的进程将不会告诉您任何信息。您需要一台单独的计算机(或像pingdom这样的第三方提供商)来提醒您这种情况。

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.

这篇关于守护程序vs Upstart for python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆