在 Linux 中守护 python 脚本的最简单方法是什么?

发布于 2024-07-05 11:33:20 字数 81 浏览 6 评论 0原文

在 Linux 中守护 python 脚本的最简单方法是什么? 我需要它适用于各种版本的 Linux,因此它应该只使用基于 python 的工具。

What would be the simplest way to daemonize a python script in Linux ? I need that this works with every flavor of Linux, so it should only use python based tools.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

能否归途做我良人 2024-07-12 11:33:20

我最近使用过 Turkmenbashi

$ easy_install turkmenbashi
import Turkmenbashi

class DebugDaemon (Turkmenbashi.Daemon):

    def config(self):
        self.debugging = True

    def go(self):
        self.debug('a debug message')
        self.info('an info message')
        self.warn('a warning message')
        self.error('an error message')
        self.critical('a critical message')

if __name__=="__main__":
    d = DebugDaemon()
    d.config()
    d.setenv(30, '/var/run/daemon.pid', '/tmp', None)
    d.start(d.go)

I have recently used Turkmenbashi :

$ easy_install turkmenbashi
import Turkmenbashi

class DebugDaemon (Turkmenbashi.Daemon):

    def config(self):
        self.debugging = True

    def go(self):
        self.debug('a debug message')
        self.info('an info message')
        self.warn('a warning message')
        self.error('an error message')
        self.critical('a critical message')

if __name__=="__main__":
    d = DebugDaemon()
    d.config()
    d.setenv(30, '/var/run/daemon.pid', '/tmp', None)
    d.start(d.go)
挽袖吟 2024-07-12 11:33:20

使用 grizzled.os.daemonize

$ easy_install grizzled

>>> from grizzled.os import daemonize
>>> daemon.daemonize()

了解其工作原理或自行操作,阅读有关 ActiveState 的讨论

Use grizzled.os.daemonize:

$ easy_install grizzled

>>> from grizzled.os import daemonize
>>> daemon.daemonize()

To understand how this works or to do it yourself, read the discussion on ActiveState.

信愁 2024-07-12 11:33:20

如果您不关心实际的讨论(这些讨论往往会偏离主题并且不提供权威的回应),您可以选择一些可以让您更轻松地品味的库。 我建议看一下 ll-xist,这个库包含大量拯救生命的代码,如 cron 作业助手、守护进程框架,以及(您对此不感兴趣,但确实很棒)面向对象的 XSL(ll-xist 本身)。

If you do not care for actual discussions (which tend to go offtopic and do not offer authoritative response), you can choose some library that will make your tast easier. I'd recomment taking a look at ll-xist, this library contains large amount of life-saving code, like cron jobs helper, daemon framework, and (what is not interesting to you, but is really great) object-oriented XSL (ll-xist itself).

不念旧人 2024-07-12 11:33:20

请参阅 Stevens 以及此 关于 activestate 的冗长线程,我个人认为这大多是不正确的,而且非常冗长,我想出了这个:

from os import fork, setsid, umask, dup2
from sys import stdin, stdout, stderr

if fork(): exit(0)
umask(0) 
setsid() 
if fork(): exit(0)

stdout.flush()
stderr.flush()
si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
se = file('/dev/null', 'a+', 0)
dup2(si.fileno(), stdin.fileno())
dup2(so.fileno(), stdout.fileno())
dup2(se.fileno(), stderr.fileno())

如果您需要再次停止该进程,则这是必需的要了解 pid,通常的解决方案是 pidfiles。 如果您需要一个,请执行此操作

from os import getpid
outfile = open(pid_file, 'w')
outfile.write('%i' % getpid())
outfile.close()

出于安全原因,您可以在妖魔化后考虑其中任何一个

from os import setuid, setgid, chdir
from pwd import getpwnam
from grp import getgrnam
setuid(getpwnam('someuser').pw_uid)
setgid(getgrnam('somegroup').gr_gid)
chdir('/') 

您也可以使用 nohup 但这不适用于 python 的子进程模块

See Stevens and also this lengthy thread on activestate which I found personally to be both mostly incorrect and much to verbose, and I came up with this:

from os import fork, setsid, umask, dup2
from sys import stdin, stdout, stderr

if fork(): exit(0)
umask(0) 
setsid() 
if fork(): exit(0)

stdout.flush()
stderr.flush()
si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
se = file('/dev/null', 'a+', 0)
dup2(si.fileno(), stdin.fileno())
dup2(so.fileno(), stdout.fileno())
dup2(se.fileno(), stderr.fileno())

If you need to stop that process again, it is required to know the pid, the usual solution to this is pidfiles. Do this if you need one

from os import getpid
outfile = open(pid_file, 'w')
outfile.write('%i' % getpid())
outfile.close()

For security reasons you might consider any of these after demonizing

from os import setuid, setgid, chdir
from pwd import getpwnam
from grp import getgrnam
setuid(getpwnam('someuser').pw_uid)
setgid(getgrnam('somegroup').gr_gid)
chdir('/') 

You could also use nohup but that does not work well with python's subprocess module

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文