python 自动重启服务
来源:互联网 发布:java new thread 编辑:程序博客网 时间:2024/05/10 17:17
1、判断记事本服务是否运行,如果没有运行,则启动记事本
配置文件:config.ini
[MonitorProgramPath]ProgramPath: C:\Windows\System32\notepad.exe[MonitorProcessName]ProcessName: notepad.exe
python脚本:
#-*- encoding: utf-8 -*-import loggingimport wmiimport osimport timefrom configparser import ConfigParserCONFIGFILE = 'config.ini'config = ConfigParser()config.read(CONFIGFILE)ProgramPath = config.get('MonitorProgramPath','ProgramPath')ProcessName = config.get('MonitorProcessName','ProcessName')#读取配置文件中的进程名和系统路径,这2个参数都可以在配置文件中修改ProList = []#定义一个列表c = wmi.WMI()def main(): for process in c.Win32_Process(): ProList.append(str(process.Name))#把所有任务管理器中的进程名添加到列表 if ProcessName in ProList:#判断进程名是否在列表中,如果是True,则所监控的服务正在 运行状态,#打印服务正常运行 print('') print("Server is running...") print('') else:#如果进程名不在列表中,即监控的服务挂了,则在log文件下记录日志#日志文件名是以年月日为文件名 f=open('.\\log\\'+time.strftime("%Y%m%d", time.localtime())+'-exception.txt','a') print('Server is not running,Begining to Restart Server...')#打印服务状态 f.write('\n'+'Server is not running,Begining to Restart Server...'+'\n') f.write(time.strftime('%Y-%m-%d %H:%M:%S --%A--%c', time.localtime()) +'\n') #写入时间和服务状态到日志文件中 os.startfile(ProgramPath)#调用服务重启 f.write('Restart Server Success...'+'\n') f.write(time.strftime('%Y-%m-%d %H:%M:%S --%A--%c', time.localtime())) f.close()#关闭文件 print('Restart Server Success...') print(time.strftime('%Y-%m-%d %H:%M:%S --%A--%c', time.localtime())) del ProList[:]#清空列表,否则列表会不停的添加进程名,会占用系统资源if __name__=="__main__" : while True: main() time.sleep(10)#每隔10秒调用脚本看下服务是否正常,如果不正常则重启服务,如果正常,则打印服务正常
0 0
- python 自动重启服务
- NODEjs自动重启服务
- monit 监控并自动重启服务
- monit设置服务自动重启
- Android 服务Service自动重启研究
- monit 监控并自动重启服务
- nodejs 自动重启服务 supervisor
- Linux下tomcat服务自动重启
- crontab命令 自动重启服务
- docker服务重启后自动重启容器
- centos设置服务自动重启
- 自动重启路由器 AutoIt + selenium + python
- python监测mysql,并自动重启
- shell 实现的自动无限重启脚本,出错自动重启服务
- linux服务器重启后,自动重启web项目
- 重启系统的时候自动启动oracle服务
- 检测tomcat服务状态,自动重启Tomcat
- 检测tomcat服务状态,自动重启Tomcat
- iOS App性能优化
- perl中, 用于stdin时的foreach与while的细微差别
- 剑指offer 60 - 把二叉树打印成多行
- C++容器类和Qt容器类的对比
- JAVA自定义注释(Target,Retention,Documented,Inherit)
- python 自动重启服务
- jacod实现转换PDF(只支持windowsx64和x84)
- 两个栈实现一个队列的添加、删除功能
- js各种验证文本框输入格式(正则表达式)
- OpenGL ES 2.0 Shader
- 静态语言 vs 动态语言
- 用NODEJS处理EXCEL文件导入导出,文件上传
- CString 类字符串操作
- js各种验证文本框输入格式(正则表达式)