内容简介:总有一些程序在windows平台表现不稳定,动不动一段时间就无响应,但又不得不用,每次都是发现问题了手动重启,现在写个脚本定时检测进程是否正常,自动重启。
总有一些程序在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)
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 去哪里找 Java 练手项目?
- 一个适合初级 Gopher 练手的项目
- 简洁的天气预报项目,可用于练手
- golang练手小项目系列(1)-位向量
- golang练手小项目系列(2)-并发爬虫
- 3道趣味Python题,非常适合菜鸟练手
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
光线跟踪算法技术
萨芬 / 刘天慧 / 清华大学出版社 / 2011-3 / 98.00元
《光线跟踪算法技术》详细阐述了与光线跟踪问题相关的高效解决方案及相应的数据结构和算法,主要包括采样技术、投影视图、视见系统、景深、非线性投影、立体视觉、光照与材质、镜面反射、光泽反射、全局光照、透明度、阴影、环境遮挡、区域光照、光线与对象间的相交计算、对象变换、栅格技术以及纹理映射技术等内容。此外,《光线跟踪算法技术》还提供了相应的算法、代码以及伪代码,以帮助读者进一步理解计算方案的实现过程。 ......一起来看看 《光线跟踪算法技术》 这本书的介绍吧!
RGB转16进制工具
RGB HEX 互转工具
在线进制转换器
各进制数互转换器