实现完整的Python Unix风格的守护进程 [英] Implementing a full Python Unix-style daemon process

查看:72
本文介绍了实现完整的Python Unix风格的守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试手动创建一个简单的守护进程,我不想使用现有的外部库来避免开销。

I'm currently trying to manually create a simple daemon process, I don't want to use the existing externals libraries to avoid overhead.

我目前检查我的进程何时运行,是否尚未创建PID文件(意味着它正在运行),如这篇文章中所述

I'm currently checking when my process runs that it doesn't have a PID file already created (meaning it's running), like described in this post.

我还有一个 daemonizing 模块,用于将PID与当前进程分离,并重定向stdout和stderr(因此,我的守护进程将继续运行,甚至如果我结束会话):

I also have a daemonizing module to detach the PID from current process and redirect stdout and stderr (so my daemon will keep running even if I end my session):

import os
import sys

def daemonize(stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
    try: 
        pid = os.fork() 
        if pid > 0:
            sys.exit(0)
    except OSError, e: 
        sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror) )
        sys.exit(1)

    os.chdir("/") 
    os.umask(0) 
    os.setsid() 

    try: 
        pid = os.fork() 
        if pid > 0:
            sys.exit(0)
    except OSError, e: 
        sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) )
        sys.exit(1)

    stdin_par = os.path.dirname(stdin)
    stdout_par = os.path.dirname(stdout)
    stderr_par = os.path.dirname(stderr)
    if not stdin_par:
        os.path.makedirs(stdin_par)
    if not stdout_par:
        os.path.makedirs(stdout_par)
    if not stderr_par:
        os.path.makedirs(stderr_par)

    si = open(stdin, 'r')
    so = open(stdout, 'a+')
    se = open(stderr, 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

因此,目前我可以像下面的示例行那样运行我的进程,它将正确运行我的守护程序:

So currently I can run my process like sample line below and it will run my daemon correctly:

>$ python myapp.py

但是到st操作上,我必须grep PID(或从锁定文件中获取它)并在以下情况后手动擦除PID:

But to stop it, I have to grep the PID (or take it from the lock file) and manually erase the PID after:

>$ ps -ef | grep myapp
xxxxx  11901     1  0 19:48 ?        00:00:00 python src/myapp.py
xxxxx  12282  7600  0 19:54 pts/7    00:00:00 grep myapp
>$ kill -9 11901
>$ rm -rf /path/to/lock.pid

我想拥有更多的Unix-就像守护程序一样,我可以使用以下命令管理守护程序生命周期:

I'd like to have a more Unix-like daemon where I can manage the daemon lifecycle with the following commands:

>$ python myapp.py start
>$ python myapp.py stop
>$ python myapp.py restart

I可以使用 argparse 模块来做到这一点,但这似乎有点乏味和丑陋。

I can certainly do it with the argparse module, but that seems a bit tedious and ugly.

你知道吗

推荐答案

看了一些之后,我发现了一个很好的例子此处

After looking a bit more I found a great example that does just this here.

它使用通用的 Daemon 类,此类可以在之后进行子类化:

It uses a generic Daemon class, which can be subclassed afterwards:

#!/usr/bin/env python

import sys, os, time, atexit
from signal import SIGTERM

class Daemon:
        """
        A generic daemon class.

        Usage: subclass the Daemon class and override the run() method
        """
        def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
                self.stdin = stdin
                self.stdout = stdout
                self.stderr = stderr
                self.pidfile = pidfile

        def daemonize(self):
                """
                do the UNIX double-fork magic, see Stevens' "Advanced
                Programming in the UNIX Environment" for details (ISBN 0201563177)
                http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
                """
                try:
                        pid = os.fork()
                        if pid > 0:
                                # exit first parent
                                sys.exit(0)
                except OSError, e:
                        sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
                        sys.exit(1)

                # decouple from parent environment
                os.chdir("/")
                os.setsid()
                os.umask(0)

                # do second fork
                try:
                        pid = os.fork()
                        if pid > 0:
                                # exit from second parent
                                sys.exit(0)
                except OSError, e:
                        sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
                        sys.exit(1)

                # redirect standard file descriptors
                sys.stdout.flush()
                sys.stderr.flush()
                si = file(self.stdin, 'r')
                so = file(self.stdout, 'a+')
                se = file(self.stderr, 'a+', 0)
                os.dup2(si.fileno(), sys.stdin.fileno())
                os.dup2(so.fileno(), sys.stdout.fileno())
                os.dup2(se.fileno(), sys.stderr.fileno())

                # write pidfile
                atexit.register(self.delpid)
                pid = str(os.getpid())
                file(self.pidfile,'w+').write("%s\n" % pid)

        def delpid(self):
                os.remove(self.pidfile)

        def start(self):
                """
                Start the daemon
                """
                # Check for a pidfile to see if the daemon already runs
                try:
                        pf = file(self.pidfile,'r')
                        pid = int(pf.read().strip())
                        pf.close()
                except IOError:
                        pid = None

                if pid:
                        message = "pidfile %s already exist. Daemon already running?\n"
                        sys.stderr.write(message % self.pidfile)
                        sys.exit(1)

                # Start the daemon
                self.daemonize()
                self.run()

        def stop(self):
                """
                Stop the daemon
                """
                # Get the pid from the pidfile
                try:
                        pf = file(self.pidfile,'r')
                        pid = int(pf.read().strip())
                        pf.close()
                except IOError:
                        pid = None

                if not pid:
                        message = "pidfile %s does not exist. Daemon not running?\n"
                        sys.stderr.write(message % self.pidfile)
                        return # not an error in a restart

                # Try killing the daemon process       
                try:
                        while 1:
                                os.kill(pid, SIGTERM)
                                time.sleep(0.1)
                except OSError, err:
                        err = str(err)
                        if err.find("No such process") > 0:
                                if os.path.exists(self.pidfile):
                                        os.remove(self.pidfile)
                        else:
                                print str(err)
                                sys.exit(1)

        def restart(self):
                """
                Restart the daemon
                """
                self.stop()
                self.start()

        def run(self):
                """
                You should override this method when you subclass Daemon. It will be called after the process has been
            daemonized by start() or restart().
                """

一旦有了此模块,就可以执行以下操作守护程序的不同模式:

Once you have this module, you can do the following to have all different modes of your daemon:

#!/usr/bin/env python

import sys, time
from daemon import Daemon

class MyDaemon(Daemon):
        def run(self):
                while True:
                        time.sleep(1)

if __name__ == "__main__":
        daemon = MyDaemon('/tmp/daemon-example.pid')
        if len(sys.argv) == 2:
                if 'start' == sys.argv[1]:
                        daemon.start()
                elif 'stop' == sys.argv[1]:
                        daemon.stop()
                elif 'restart' == sys.argv[1]:
                        daemon.restart()
                else:
                        print "Unknown command"
                        sys.exit(2)
                sys.exit(0)
        else:
                print "usage: %s start|stop|restart" % sys.argv[0]
                sys.exit(2)

希望帮助其他陷入同一问题的人!

Hope that helps other people who get stuck on the same problem !

这篇关于实现完整的Python Unix风格的守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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