使用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);
新闻热点
疑难解答