python 编写守护进程(后台运行)

转自:https://www.cnblogs.com/qianchengprogram/p/6605675.html

import os

if __name__ == '__main__':
    pid=os.fork()
    if pid != 0:
        os._exit(0)
    else:
        myqueue=Queue.Queue(maxsize=10)
        a=file_read('/usr/local/nginx/logs/access.log',myqueue)
        #a=file_read('/tmp/test1.log',myqueue)
        #p1 = multiprocessing.Process(target = a.file_readline)
        a1=threading.Thread(target = a.timecheck)
        a1.start()
        p1=threading.Thread(target = a.file_readline)
        p1.start()

再也不用nohup或者setsid了,这段代码主进程fork出子进程,然后会自动退出,子进程会执行else后面的语句,随便各位放点什么。

猜你喜欢

转载自blog.csdn.net/zhangshoug/article/details/81168977