python定时检测无响应进程并重启的实例代码
作者:零壹视界 发布时间:2023-11-29 11:00:39
标签: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的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回。使用property修饰的实例
- 项目有时要用一些Ajax的效果,因为比较简单,也就没有去用什么Ajax.net之类的东西,手写代码也就实现了。 第二天,有人反馈错
- 运算符重载意味着赋予超出其预定义的操作含义的扩展含义。例如运算符 + 用于添加两个整数以及连接两个字符串和合并两个列表。这是可以实现的,因为
- 目录一、线程基础以及守护进程二、线程锁(互斥锁)三、线程锁(递归锁)四、死锁五、队列六、相关面试题七、判断数据是否安全八、进程池 &
- function is_email($str){ //检验email return preg_match("/
- Python中,使用for循环可以迭代容器对象中的元素,这里容器对象包括是列表(list)、元组(tuple)、字典(dict)、集合(se
- 一、分屏展示当你想同时看到多个文件的时候:右击标签页;选择 move right 或者 split vertical;效果:二、远程 Pyt
- 对于php,个人感觉能够熟练操作数组和字符串,基本上已经是入门了,php本身有很多操作数组和字符串的函数,今天在做一个功能时,需要用Js动态
- 我就废话不多说了,大家还是直接看代码吧~import datetime# 时间格式 .%f 毫秒## "%Y-%m-%dT%H:%
- 通常,为了安全性,数据库只允许通过ssh来访问。例如:mysql数据库放在服务器A上,只允许数据库B来访问,这时,我们需要用机器C去访问数据
- 一、 简单查询简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的表或视图、以及搜索
- 1、在mysql 中建立一个数据库 test1 语句:create database test1 2、创建表examinfo create
- 问题1:使用.net2005自带的SQL-Express连接不上。解决方法:1.网络防火墙阻止数据库连接;2.默认SQL-Express没有
- 使用ktl工具实现mysql向mysql同步数据1.新建作业步骤2.完善作业步骤,主要是完成作业中的转换工作3.首先要确定数据来源库,也就是
- 这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为:<!DOCTYPE html&g
- 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短
- 1.介绍Go官方提供了database包,database包下有sql/driver。该包用来定义操作数据库的接口,这保证了无论使用哪种数据
- 网页中使用flash可以增强页面的动态交互效果,特别是用flash来制作广告,效果更好。经常使用flash的人,可能就碰到了flash会遮住
- 在前面的DRF系列教程中,我们以博客为例介绍了序列化器, 使用基于类的视图APIView和ModelViewSet开发了针对文章资源进行增删
- 前言MySQL 8.0.26于2021年7月20日发布。一个变化需要注意,在这一版本里面改动了大量的变量名称,大量包含master和 sla