守护进程代码:
import time, os
import 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是一个文件,里面的每一行是要监控的exe文件路径:
eg: C:\Program Files (x86)\Jenkins\jenkins.exe
监控进程:
import time, os
import subprocess def run():
i = 0
while True:
filehandler = open(os.path.join('c:\logs', 'bollist' + str(i) + '.txt'), 'w')
a = subprocess.Popen('tasklist /fi "Imagename eq cmd.exe" /v ', stdout= filehandler)
i = i+1
time.sleep(10) run()