您的位置:首页 > 编程语言 > Python开发

python 守护进程,监控进程

2015-03-04 17:25 344 查看
守护进程代码:

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: