1.守护进程python
[1]使用runner这个模块直接建立守护进程,很是方便。app
[2]运行方法:python xxx.py start|stop|restartspa
[3]调用python xxx.py stop时,会给进程发送signal.SIGTERM信号,因此能够捕获此信号进行程序退出前的处理。rest
[4]流程:code
1)调用python xxx.py start启动一个守护进程,例如进程号是1000。blog
2)调用python xxx.py stop时,会给进程1000发送一个signal.SIGTERM信号,进程1000结束。进程
# -*- coding: utf-8 -*- import os,sys,time import threading import signal from daemon import runner class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/null' self.stderr_path = '/dev/null' self.pidfile_path = '/tmp/test.pid' self.pidfile_timeout = 5 def run(self): #在这里添加主流程 while True: time.sleep(10) if __name__ == "__main__": app = App() daemon_runner = runner.DaemonRunner(app) daemon_runner.do_action()