总有一些程序在windows平台表现不稳定,动不动一段时间就无响应,但又不得不用,每次都是发现问题了手动重启,现在写个脚本定时检测进程是否正常,自动重启。
涉及知识点
- schedule定时任务调度
- os.popen运行程序并读取解析运行结果
代码分解
脚本主入口
1
2
3
4
5
6
7
|
if __name__ = = '__main__' :
#每5秒执行检查任务
schedule.every( 5 ).seconds.do(check_job)
#此处固定写法,意思是每秒钟schedule看下是否有pending的任务,有就执行
while true:
schedule.run_pending()
time.sleep( 1 )
|
schedule的其它示例
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import schedule
import time
def job(message = 'stuff' ):
print ( "i'm working on:" , message)
#每10分钟
schedule.every( 10 ).minutes.do(job)
#每小时
schedule.every().hour.do(job, message = 'things' )
#每天10点30分
schedule.every().day.at( "10:30" ).do(job)
while true:
schedule.run_pending()
time.sleep( 1 )
|
检查无响应进程并重启
1
2
3
4
5
6
7
8
9
10
|
def check_job():
process_name = "xx.exe"
not_respond_list = list_not_response(process_name)
if len (not_respond_list) < = 0 :
return
pid_params = " " .join([ "/pid " + pid for pid in not_respond_list])
os.popen( "taskkill /f " + pid_params)
if len (list_process(process_name)) < = 0 :
start_program(r 'e:\xx\xx.exe' )
}
|
查找符合条件的进程列表
1
2
3
4
5
6
7
8
|
def list_process(process_name, not_respond = false):
cmd = 'tasklist /fi "imagename eq %s"'
if not_respond:
cmd = cmd + ' /fi "status eq not responding"'
output = os.popen(cmd % process_name)
return parse_output(output.read())
def list_not_response(process_name):
return list_process(process_name, true)
|
解析命令执行结果
1
2
3
4
5
6
7
8
|
def parse_output(output):
print (output)
pid_list = []
lines = output.strip().split( "\n" )
if len (lines) > 2 :
for line in lines[ 2 :]:
pid_list.append(line.split()[ 1 ])
return pid_list
|
tasklist示例输出
1
2
3
|
映像名称 pid 会话名 会话 # 内存使用
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
wizchromeprocess.exe 1620 console 1 32 , 572 k
|
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import os
import time
import schedule
def parse_output(output):
print (output)
pid_list = []
lines = output.strip().split( "\n" )
if len (lines) > 2 :
for line in lines[ 2 :]:
pid_list.append(line.split()[ 1 ])
return pid_list
def list_not_response(process_name):
return list_process(process_name, true)
def list_process(process_name, not_respond = false):
cmd = 'tasklist /fi "imagename eq %s"'
if not_respond:
cmd = cmd + ' /fi "status eq not responding"'
output = os.popen(cmd % process_name)
return parse_output(output.read())
def start_program(program):
os.popen(program)
def check_job():
process_name = "xx.exe"
not_respond_list = list_not_response(process_name)
if len (not_respond_list) < = 0 :
return
pid_params = " " .join([ "/pid " + pid for pid in not_respond_list])
os.popen( "taskkill /f " + pid_params)
if len (list_process(process_name)) < = 0 :
start_program(r 'e:\xxx\xx.exe' )
if __name__ = = '__main__' :
schedule.every( 5 ).seconds.do(check_job)
while true:
schedule.run_pending()
time.sleep( 1 )
|
总结
以上所述是小编给大家介绍的python定时检测无响应进程并重启的实例代码 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!原文链接:http://www.xetlab.com/2019/04/21/