daemon.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:sc-controller 作者: kozec 项目源码 文件源码
def stop(self, once=False):
        """Stop the daemon."""

        # Get the pid from the pidfile
        try:
            with open(self.pidfile, 'r') as pidf:
                pid = int(pidf.read().strip())
        except Exception:
            pid = None

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

        # Try killing the daemon process
        try:
            for x in xrange(0, 10): # Waits max 1s
                os.kill(pid, signal.SIGTERM)
                if once: break
                for x in xrange(50):
                    os.kill(pid, 0)
                    time.sleep(0.1)
                time.sleep(0.1)
            os.kill(pid, signal.SIGKILL)
        except OSError as err:
            e = str(err.args)
            if e.find("No such process") > 0:
                if os.path.exists(self.pidfile):
                    os.remove(self.pidfile)
            else:
                print(str(err.args))
                sys.exit(1)
        syslog.syslog(syslog.LOG_INFO, '{}: stopped'.format(os.path.basename(sys.argv[0])))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号