python写的进程守护者

用Python写的一个守护者,核心的代码不到10行。这个方法是由同事想到的,我把核心的代码提出来,真是太cool了:

import os, subprocess

def Run():
    while True:
        taskList = os.popen('tasklist').read()
        for path, exe in [os.path.split(line.strip()) for line in open('config') if line.strip()]:
            if exe not in taskList:
                subprocess.Popen(u'start /d"%s" %s' % (path, exe), shell = True)
        
        time.sleep(60)

Run()

config文件是守护的进程路径,如:

F:\Server\DBServer.exe
F:\Server\ChatServer.exe
F:\Server\\LogicServer.exe
F:\Server\LoginServer.exe

你可能感兴趣的:(python,OS,F#)