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/
0
投稿
猜你喜欢
- 实例如下:/** * 数字格式转换成千分位 *@param{Object}num */function commafy(num){ &nbs
- Pygame的mixer 模块可以依据命令播放一个或多个声音,并且也可以将这些声音混合在一起。而获得声音需要四个步骤:一、启动mixer进程
- 在 Python 中,列表是一种非常常见且强大的数据类型。但有时候,我们需要从一个列表中删除特定元素,尤其是当这个元素出现多次时。本文将介绍
- UserAgent = Trim(Lcase(Request.Serve
- 本文实例为大家分享了python实现多人聊天室的具体代码,供大家参考,具体内容如下刚开始学习python,写了一个聊天室练练手。Server
- jQuery居然都没有JSON的decode和encode,精确类型判断也没有,囧……自己动手写吧!不过这些东西在网上都已经有很好的版本了,
- --语 句 功 能--数据操作SELECT --从数据库表中检索数据行和列INSERT --向数据库表添加新数据行DELETE --从数据库
- 本文实例为大家分享了python实现购物车功能的具体代码,供大家参考,具体内容如下功能要求:要求用户输入总资产,例如:2000显示商品列表,
- 密码已经是我们生活工作中必不可少的工具,但一个不安全的密码有又有可能会给我们造成不必要的损失。作为网站设计者,如果我们在网页中能对用户输入的
- 写这个的目地,主要是系统理下目前产品设计的流程,提醒自己尽量去避免一些常见的问题,也能让大家系统的了解天极网的产品设计流程。当然,不保证任何
- 在写代码的时候,往往会漏掉日志这个关键因素,导致功能在使用的时候出错却无法溯源。其实,只需要写一个非常简单的日志装饰器,我们就能大大提升排查
- ①GET# -*- coding:utf-8 -*-import requestsdef get(url, datas=None): &nb
- 1. 使用默认的session, 在ini文件中:from pyramid.session import UnencryptedCookie
- 本文记录,如何使用 Python 来抓取,图片或者文件的,创建日期,修改日期1. 读取照片创建日期(._getexif())from PIL
- 这篇文章主要介绍了Python Django 简单分页的实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 代码如下: var params = new Enumerator(Request.QueryString); while (!params
- 今天终于可以用wxPython开发GUI程序了,非常高兴。把其中的一些注意点写下来以供参考。在windows XP平台下,首先需要做以下环境
- 一、相关代码数据库配置类 MongoDBConn.py#encoding=utf-8'''Mongo Conn连接类
- FrontPage 2003在功能上增强了不少,下面我们一起来看看新版本中比较突出的9个新功能。1.自定义浏览器分辨率预览检查 在
- 在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下:python [-bBdEhiIOqsSuvV