python停止执行的代码(python定时检测无响应进程并重启的实例代码)
类别:脚本大全 浏览量:2569
时间:2021-10-18 11:16:14 python停止执行的代码
python定时检测无响应进程并重启的实例代码总有一些程序在windows平台表现不稳定,动不动一段时间就无响应,但又不得不用,每次都是发现问题了手动重启,现在写个脚本定时检测进程是否正常,自动重启。
涉及知识点
- schedule定时任务调度
- os.popen运行程序并读取解析运行结果
代码分解
脚本主入口
|
if __name__ = = '__main__' : #每5秒执行检查任务 schedule.every( 5 ).seconds.do(check_job) #此处固定写法,意思是每秒钟schedule看下是否有pending的任务,有就执行 while true: schedule.run_pending() time.sleep( 1 ) |
schedule的其它示例
|
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 ) |
检查无响应进程并重启
|
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 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/
您可能感兴趣
- python pyqt 教程(Python+PyQt5实现美剧爬虫可视工具的方法)
- python3标准库资源(Python3标准库总结)
- 如何用python爬取最新电影(详解Python爬取并下载《电影天堂》3千多部电影)
- python的turtle库怎么画同心圆(详解python使用turtle库来画一朵花)
- python中startswith使用教程(Python os.access用法实例)
- python datetime模块是什么(Python中时间datetime的处理与转换用法总结)
- 如何用python识别微信内容(Python 实现微信防撤回功能)
- python如何遍历一个列表(说说如何遍历Python列表的方法示例)
- python协程解释(在python里从协程返回一个值的示例)
- python简单代码实例(Python实现 版本号对比功能的实例代码)
- python如何获取微信好友(利用Python查看微信共同好友功能的实现代码)
- python实现将txt转化为excel(python实现Excel文件转换为TXT文件)
- python撤回的微信消息怎么看(Python实现微信消息防撤回功能的实例代码)
- python批量转换图片格式(利用Python对文件夹下图片数据进行批量改名的代码实例)
- python创建一个xls文件(Python XlsxWriter模块Chart类用法实例分析)
- python时间类的实现(Python日期时间Time模块实例详解)
- 庆八一,重读经典红色语录,感悟互联网发展硬道理(重读经典红色语录)
- TVB新剧《黯夜守护者》将播,陈展鹏陈炜首次合作探讨人性(TVB新剧黯夜守护者将播)
- 新晋小花被称女版吴卓羲 将取代滕丽名成为TVB新一代御用女警(新晋小花被称女版吴卓羲)
- 艺人吴卓羲10年警察生涯,演足10年阿Sir,系咩玩法(艺人吴卓羲10年警察生涯)
- 菲律宾潜水(菲律宾潜水价格)
- 泰国人妖(变性手术生殖器要割掉吗)
热门推荐
- TransactionScope的使用
- css样式中实现3d效果(利用纯CSS实现动态的文字效果实例)
- dedecms列表样式修改(dedecms5.7sp1评论添加字段的实现方法)
- pyqt复选框样式(基于PyQt4和PySide实现输入对话框效果)
- 服务器高性能ssd是什么(为什么更推荐SSD技术的云服务器?)
- 微信浏览器中JS实现返回操作
- 阿里云虚拟主机限制ip访问次数(轻云服务器或虚拟主机站点域名解析不正确的分析与解决方法)
- ecs设置(如何解决ECS Windows提示显示设置无法从远程会话进行更改)
- python 文本文件读取方法(Python逐行读取文件中内容的简单方法)
- sql server日志备份查看(sql server数据库高可用日志传送的方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9