如何在 Linux 上管理基于 Python 的守护进程?

发布于 2024-09-04 11:55:38 字数 359 浏览 6 评论 0原文

我有一个基于 Python 的程序,我想将其作为守护进程运行。目前,我正在以一种非常黑客的方式进行操作,以 screen-d -m name 会话并使用 pkill -9 -f name 终止它。

最终我不得不将其移至我们在这里使用的更好的系统(因此我不愿意修改程序),但在此期间,我正在寻找一种更干净的方法来做这个。

我目前的想法是将其作为 inti.d 脚本的后台任务启动,但我如何编写该部分以将其恢复?

I have a working Python based program that I want to run as a daemon. Currently I'm doing it in a very hackish manner of starting it in with screen-d -m name session and killing it with pkill -9 -f name.

Eventually I'm doing to have to move this to the better system we use here (thus I'm not willing to modify the program) but in the interim, I'm looking for a cleaner way to do this.

My current thinking is kick it off as a background task from an inti.d script but how do I write the part to bring it back down?

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

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

发布评论

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

评论(6

他不在意 2024-09-11 11:55:38

在 Linux 上,有一个 start-stop-daemon 实用程序作为 init 的一部分。 d 工具。

它非常灵活,允许使用不同的方式来捕获服务器的 pid。

还有一个文件 /etc/init.d/sculpture 可以作为您自己的 init.d 脚本的基础。

如果您的目标平台是基于 debian 的,那么创建一个 debina 包来部署它是有意义的,因为它还有助于将守护程序正确集成到系统的其余部分中。而且它并不太复杂(如果你之前已经做过十次了;-)

On linux there is a start-stop-daemon utility as part of the init.d tools.

It is very flexible and allows different ways for capturing the pid of your server.

There is also a file /etc/init.d/skeleton which can serve as a basis for your own init.d script.

If your target platform is debian based, it makes sense to create a debina package to deploy it as it also helps getting a daemon properly integrated in the rest of the system. And it is not too complicated (if you have done it ten times before ;-)

情释 2024-09-11 11:55:38

如果您想使用 python 代码来完成此操作,这是一个非常标准的 C 方法,已移植到我使用的 python 中。它工作完美,您甚至可以选择文件输出。

import os
import signal
def daemonize(workingdir='.', umask=0,outfile='/dev/null'):
#Put in background
pid = os.fork()
if pid == 0:
    #First child
    os.setsid()
    pid = os.fork() #fork again
    if pid == 0:
        os.chdir(workingdir)
        os.umask(umask)
    else:
        os._exit(0)
else:
    os._exit(0)

#Close all open resources
try:
    os.close(0)
    os.close(1)
    os.close(2)
except:
    raise Exception("Unable to close standard output. Try running with 'nodaemon'")
    os._exit(1)

#Redirect output
os.open(outfile, os.O_RDWR | os.O_CREAT)
os.dup2(0,1)
os.dup2(0,2)

然后,您可以使用信号来捕获终止信号何时发送到程序并顺利退出。来自 Python 文档 的示例

import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

If you want to do it with code in python, this is a pretty standard C-method that was ported to python that I use. It works flawlessly, and you can even choose a file output.

import os
import signal
def daemonize(workingdir='.', umask=0,outfile='/dev/null'):
#Put in background
pid = os.fork()
if pid == 0:
    #First child
    os.setsid()
    pid = os.fork() #fork again
    if pid == 0:
        os.chdir(workingdir)
        os.umask(umask)
    else:
        os._exit(0)
else:
    os._exit(0)

#Close all open resources
try:
    os.close(0)
    os.close(1)
    os.close(2)
except:
    raise Exception("Unable to close standard output. Try running with 'nodaemon'")
    os._exit(1)

#Redirect output
os.open(outfile, os.O_RDWR | os.O_CREAT)
os.dup2(0,1)
os.dup2(0,2)

Then, you can use signals to catch when a kill-signal was sent to the program and exit nicely. Example from Python Docs

import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm
小猫一只 2024-09-11 11:55:38

有些模块可用于守护 python 脚本。

python-daemon 实现了行为良好的守护进程规范 (PEP 3143)。

另外这个模块最近出现在github上,它看起来更Pythonic并且易于使用。

There are modules that could be used to daemonize a python script.

python-daemon implements the well-behaved daemon specification (PEP 3143).

Also this module recently came up on github which seems more pythonic and easy to use.

温馨耳语 2024-09-11 11:55:38

使用 init.d 样式脚本启动它是一个好方法。您可以使用 POSIX Signals 将其取下...请参阅 StackOverflow,Python 中的信号处理

Starting it with an init.d style script is a good way. You take it down with POSIX Signals ... See StackOverflow, Signal handling in Python.

我不在是我 2024-09-11 11:55:38

尝试这个问题或更准确地接受的解决方案。

Try this question or more exactly accepted solution.

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