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开启两个线程(Python开启线程,在函数中开线程的实例)
- python爬虫入门自学(自学python爬虫的建议和周期预算)
- python装饰器怎么编写(Python函数装饰器常见使用方法实例详解)
- python语言中的递归(详解python中递归函数)
- 用python3.5.3实现邮件收发(Python使用POP3和SMTP协议收发邮件的示例代码)
- pythonexcel生成报表(python生成每日报表数据Excel并邮件发送的实例)
- Python实现FTP弱口令扫描器的方法示例(Python实现FTP弱口令扫描器的方法示例)
- 怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)
- 怎么用python做随机矩阵(python实现杨氏矩阵查找)
- python气温变化数据分析(基于python历史天气采集的分析)
- python入门之字符串处理(Python中常用的8种字符串操作方法)
- pythontkinter循环显示文本(Python实现定时自动关闭的tkinter窗口方法)
- python生成文本文件(python+os根据文件名自动生成文本)
- python中对象方法和顶级方法(Python3.5面向对象程序设计之类的继承和多态详解)
- python批量注册(python实现批量注册网站用户的示例)
- python怎么测试api接口(python接口自动化测试之接口数据依赖的实现方法)
- 《道德经》:功成不局,泰而不骄(道德经:功成不局)
- 每日一典 过江之鲫(每日一典过江之鲫)
- 红色代表什么(红色代表什么意义和象征)
- 菲律宾安全吗(菲律宾安全吗2023)
- 彩礼重要吗()
- 写信告诉我今天海是什么颜色(写信告诉我今天海是什么颜色回答)
热门推荐
- php 设置sessionid生成机制(PHP Cli 模式设置进程名称的方法)
- php生成md5(php进行md5加密简单实例方法)
- C#不建议在构造函数中调用虚方法或虚属性
- pythonbug生成图片(python实现海螺图片的方法示例)
- dockerservice启动参数(docker.service启动报错的一次排查详解)
- js轮播图片(JS实现简单图片轮播效果)
- 腾讯云服务器如何安装宝塔面板(腾讯云服务器打开宝塔面板失败是怎么回事?)
- 怎么样查找linux的ssl证书(护卫神主机大师Linux添加绑定ssl证书图文教程)
- border和background区别(css中background-origin属性的使用解析)
- laravel查询数据库视图(Laravel5.7 数据库操作迁移的实现方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9