python实现监控windows服务并自动启动服务示例

使用python 2.7 + pywin32 + wxpython开发

每隔一段时间检测一下服务是否停止,如果停止尝试启动服务。进行服务停止日志记录

appmain.py

代码如下:

#!/usr/bin/env python#-*- encoding:utf-8 -*-

“””1. 每隔一分钟检测一次服务状态2. 如果发现服务状态已经停止,那么尝试启动服务3. 自动记录日志4. 任务栏图标显示”””

import sys;reload(sys);sys.setdefaultencoding(‘utf-8’);

import win32service;import logging;from logging.handlers import rotatingfilehandler;import os.path;import wx;import appresource;import webbrowser;from appxml import *;

c_app_name = “service moniter 1.0”;c_log_dir = os.path.altsep.join([os.path.curdir,’service.log’]);c_config_path = os.path.altsep.join([os.path.curdir,’config.xml’]);c_log_size = 1048576;c_log_files = 3;

c_app_site = “http://www.du52.com/?app=service_moniter&version=1.0.0”;

class servicecontrol(object):

def __init__(self): self.scm = win32service.openscmanager(none,none,win32service.sc_manager_all_access);

# 检查服务是否停止 def isstop(self,name): flag = false; try: handle = win32service.openservice(self.scm,name,win32service.sc_manager_all_access); if handle: ret = win32service.queryservicestatus(handle); flag = ret[1]!=win32service.service_running; win32service.closeservicehandle(handle); except exception,e: logging.error(e); return flag;

# 开启服务 def start(self,name): try: handle = win32service.openservice(self.scm,name,win32service.sc_manager_all_access); if handle: win32service.startservice(handle,none); win32service.closeservicehandle(handle); except exception,e: logging.error(e);

# 退出 def close(self): try: if self.scm: win32service.closeservicehandle(self.scm); except exception,e: logging.error(e);

# 初始化日志def initlog(): logging.getlogger().setlevel(logging.error); rthandler = rotatingfilehandler(filename=c_log_dir,maxbytes=c_log_size,backupcount=c_log_files); rthandler.setlevel(logging.error); rthandler.setformatter(logging.formatter(‘[%(asctime)s][%(levelname)s] %(message)s’)); logging.getlogger().addhandler(rthandler); logging.error(‘监控开始执行’);

# 系统托盘图标class taskicon(wx.taskbaricon):

def __init__(self): wx.taskbaricon.__init__(self); self.seticon(appresource.taskicon.geticon(),c_app_name); self.id_name = wx.newid(); self.id_exit = wx.newid(); self.id_author = wx.newid(); self.bind(wx.evt_menu,self.onexitevent,id=self.id_exit); self.bind(wx.evt_menu,self.onhelpevent,id=self.id_author);

def onhelpevent(self,event): webbrowser.open_new(c_app_site);

def onexitevent(self,event): wx.exit();

def createpopupmenu(self,event=none): menu = wx.menu(); menu.append(self.id_name,c_app_name); menu.appendseparator(); menu.append(self.id_author,”技术支持”); menu.append(self.id_exit,”退出”); return menu;

# 隐藏窗口class frame(wx.frame):

def __init__(self,timelen,services): wx.frame.__init__(self,parent=none,title=c_app_name); self.timelen = timelen*1000; self.services = services; self.show(false); self.bind(wx.evt_timer,self.ontimerevent); self.bind(wx.evt_close,self.onexitevent); self.timer = wx.timer(self); self.timer.start(self.timelen);

def ontimerevent(self,event): sc = servicecontrol(); for name in self.services: print name; if sc.isstop(name): logging.error(‘系统检测到服务[%s]停止’%(name,)); sc.start(name); sc.close();

def onexitevent(self,event): if self.timer: self.timer.stop(); self.timer = none;

# 进程class application(wx.app):

def oninit(self): # 初始化配置 xml = xmlnode(); if not xml.loadfile(c_config_path): logging.error(‘配置文件不存在’); return false; timelen = xml.findnode(‘time’).getint(); if timelen

Posted in 未分类