Linux 守护进程

import os

import sys

import time

import subprocess

 

def get_process_id(name):

    """查询进程ID""" 

    child = subprocess.Popen(["pgrep","-f",name],stdout=subprocess.PIPE,shell=False)

    pid = child.communicate()[0]

    return pid

 

while True: 

pid = get_process_id("python test.py") # 要守护的程序文件

    if not pid:

        print 'not pid, Reboot'

        os.system("nohup python test.py &") # 重新开启进程

    time.sleep(60) 

 


posted @ 2018-09-19 15:34  python许三多  阅读(349)  评论(0编辑  收藏  举报