使用Windows的wmic命令,获取可执行文件的运行状况、文件路径、PID,如果可执行文件挂掉,就重启并邮件告警。
因为监控的可执行文件的文件名一样,不好区分,所以我使用文件的绝对路径为标准来判断是否正常运行,代码及详细解释如下:
# -*- coding: utf-8 -*-
import os
import win32api
import smtplib
from email.mime.text import MIMEText
def get_pidWay(file_name):
ept_list = []
temp_list = []
pid_way = os.popen("wmic process where name='" + file_name + "' get processid,executablepath,name").readlines()
for j in pid_way:
temp_list.append(j.split())
while ept_list in temp_list:
temp_list.remove(ept_list)
return(temp_list)
def open_file(filePath):
win32api.ShellExecute(0, 'open', filePath, '','',1)
def mailsend (mailtext,mailsubject):
mailserver = "smtp.qq.com"
user