繁体   English   中英

Python - os.kill(pid,SIGTERM)正在使我的进程变成僵尸

[英]Python - os.kill(pid, SIGTERM) is causing my process to become a zombie

我有一个启动daemon进程的Python脚本。 我可以使用以下代码找到这个代码: https//gist.github.com/marazmiki/3618191

代码完全按预期启动daemon进程。 但是, 有时候有时只有在daemon进程停止时,正在运行的作业才会被占用。

代码的stop功能是:

    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:
            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(1.0)
        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)

当这个stop()方法运行时,进程( pid )似乎挂起,当我Control+C输出时,我看到脚本是time.sleep(1.0)KeyboardInterrupted ,这让我相信这行:

os.kill(pid, SIGTERM)

是有问题的代码。

有谁知道为什么会发生这种情况? 为什么这个os.kill()会迫使进程变成僵尸?

我在Ubuntu linux上运行它(如果它很重要)。

更新 :我按@ paulus的答案包含了我的start()方法。

    def start(self):
        """
            Start the daemon
        """
        pid = None
        # 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:
            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()

更新2 :这是daemonize()方法:

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()

        sys.stdout = file(self.stdout, 'a+', 0)
        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)

你看错了方向。 有缺陷的代码不是停止例程中的代码,而是在开始代码中(如果您使用的是gist中的代码)。 Double fork是一个正确的方法,但是第一个fork应该等待子进程,而不是简单地退出。

可以在此处找到正确的命令序列(以及执行双叉的原因): http//lubutu.com/code/spawning-in-unix (请参阅“双叉”部分)。

有时你提到的是当第一个父母在获得SIGCHLD之前去世而它没有进入init时。

据我所知,除了信号处理之外,init还应该定期从它的子节点读取退出代码,但新手版本只依赖于后者(因此问题,请参阅类似bug的评论: https//bugs.launchpad.net / upstart / + bug / 406397 / comments / 2 )。

所以解决方案是重写第一个fork来实际等待孩子。

更新:好的,你想要一些代码。 这就是:pastebin.com/W6LdjMEz我已经更新了daemonize,fork和start方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM