网站首页 > 技术文章 正文
最近用python开发了一些程序后跑在一台专门服务器上,部分程序在运行一段时间会莫名地关闭,分析估计是程序遇到一些未知情况导致自动关闭,为了防止出现这个问题,考虑能不能开发个软件,时刻监控批量指定程序的运行,一旦发现进程中没有这个程序,就自动重启这个程序,经过一段时间百度搜索学习借鉴,终于做出来了,本人给程序起名:程序守候。
该程序原理如下:
1、获取指定文件夹所有程序清单和程序路径:
def getFiles(path):
if path[-1:] != '\\':
path += '\\'
path=path+"jivy\\"
dirFiles = os.listdir(path)
for file in dirFiles:
filePath = path + file
if SUFFIX == '' or file[-4:] == SUFFIX:
files.append([file,filePath]
2、将程序清单和进程清单进行一一对比,如果进程中没有该程序,就启动该程序
def shouhou(ProcessName,ProgramPath):
for process in c.Win32_Process():
ProList.append(str(process.Name))
if ProcessName in ProList:
pass
# 判断进程名是否在进程清单中,如果没有,就重启该程序
else:
print(ProcessName +"已停止,正在重新启动服务。。。")
os.startfile(ProgramPath)
print("重新启动"+ProcessName +"成功。。。")
print(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()))
del ProList[:]
启动程序,重复刷新文件夹和进程
if __name__ == '__main__':
print("程序守候 Design by jivy ")
print("该程序能自动监护程序目录“jivy”文件夹里面所有程序运行,程序意外中止可自动重启 ")
while 1:
ProList = []
files = []
try:
getFiles(PATH)
for fff in files:
shouhou(fff[0],fff[1])
time.sleep(60) #每60秒刷新一次
except:
pass
汇总代码如下,供大家学习:
# -*- coding: utf-8 -*-
# Design by jivy
import wmi
import os
import time
ProList = []
c = wmi.WMI()
PATH=os.getcwd()
SUFFIX = '.exe' #查找.exe文件
files = []
result = []
def getFiles(path):
if path[-1:] != '\\':
path += '\\'
path=path+"jivy\\"
dirFiles = os.listdir(path)
for file in dirFiles:
filePath = path + file
if SUFFIX == '' or file[-4:] == SUFFIX:
files.append([file,filePath])
def shouhou(ProcessName,ProgramPath):
for process in c.Win32_Process():
ProList.append(str(process.Name))
if ProcessName in ProList:
pass
# 判断进程名是否在进程清单中,如果没有,就重启该程序
else:
print(ProcessName +"已停止,正在重新启动服务。。。")
os.startfile(ProgramPath)
print("重新启动"+ProcessName +"成功。。。")
print(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()))
del ProList[:]
if __name__ == '__main__':
print("程序守候 Design by jivy ")
print("该程序能自动监护程序目录“jivy”文件夹里面所有程序运行,程序意外中止可自动重启 ")
while 1:
ProList = []
files = []
try:
getFiles(PATH)
for fff in files:
shouhou(fff[0],fff[1])
time.sleep(60) #每60秒刷新一次
except:
pass
说明:需将py封装成EXE,否则进程中无法识别进程名称。
猜你喜欢
- 2024-12-19 提高效率的 10 个 Python 调试技巧
- 2024-12-19 python-IO多路复用(select、poll、epoll)
- 2024-12-19 一篇文章搞懂 Python select 模块
- 2024-12-19 每个python人都离不开的12个python库
- 2024-12-19 使用 Python 和 OpenCV 进行面部识别 - 应用于监控、人脸门禁和考勤等
- 2024-12-19 使用 Python3 uWSGI 实现并发和监控的 Web 应用部署(44)
- 2024-12-19 Python 自动化: eip、cen监控数据对接到 grafana
- 2024-12-19 python 你需要知道的
- 2024-12-19 监控老板一举一动 99行python助你无风险摸鱼
- 2024-12-19 大神教你如何用python监听软件
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)