python定时检测无响应进程并重启的实例代码
Python  /  管理员 发布于 7年前   196
总有一些程序在windows平台表现不稳定,动不动一段时间就无响应,但又不得不用,每次都是发现问题了手动重启,现在写个脚本定时检测进程是否正常,自动重启。
涉及知识点
代码分解
脚本主入口
if __name__ == '__main__': #每5秒执行检查任务 schedule.every(5).seconds.do(check_job) #此处固定写法,意思是每秒钟schedule看下是否有pending的任务,有就执行 while True: schedule.run_pending() time.sleep(1)
schedule的其它示例
import scheduleimport timedef 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)
检查无响应进程并重启
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')}
查找符合条件的进程列表
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)
解析命令执行结果
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示例输出
映像名称PID 会话名 会话# 内存使用========================= ======== ================ =========== ============WizChromeProcess.exe 1620 Console 1 32,572 K
完整代码
import osimport timeimport scheduledef 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_listdef 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定时检测无响应进程并重启的实例代码 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号