#-*- coding:utf-8 -*-
import win32serviceutil
import win32service
import win32event
class PythonService(win32serviceutil.ServiceFramework):
"""
Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
Options for 'install' and 'update' commands only:
--username domain\username : The Username the service is to run under
--password password : The password for the username
--startup [manual|auto|disabled|delayed] : How the service starts, default = manual
--interactive : Allow the service to interact with the desktop.
--perfmonini file: .ini file to use for registering performance monitor data
--perfmondll file: .dll file to use when querying the service for
performance data, default = perfmondata.dll
Options for 'start' and 'stop' commands only:
--wait seconds: Wait for the service to actually start or stop.
If you specify --wait with the 'stop' option, the service
and all dependent services will be stopped, each waiting
the specified period.
"""
# 服务名
_svc_name_ = "PythonService"
# 服务显示名称
_svc_display_name_ = "Python Service Demo"
# 服务描述
_svc_description_ = "Python service demo."
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.isAlive = True
def _getLogger(self):
import logging
import os
import inspect
# 创建一个logger,设置日志名称,为我们的模块名
# 返回一个logger实例,如果没有指定name,返回root logger。
# 只要name相同,返回的logger实例都是同一个而且只有一个,
# 即name和logger实例是一一对应的。这意味着,
# 无需把logger实例在各个模块中传递。只要知道name,
# 就能得到同一个logger实例。
logger = logging.getLogger('[PythonService]')
# 获取当前文件夹路径
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
# 创建一个handler,用于写入日志文件
# StreamHandler: 输出到控制台
# FileHandler: 输出到文件
# BaseRotatingHandler 可以按时间写入到不同的日志中。比如将日志按天写入不同的日期结尾的文件文件。
# SocketHandler 用TCP网络连接写LOG
# DatagramHandler 用UDP网络连接写LOG
# SMTPHandler 把LOG写成EMAIL邮寄出去
handler = logging.FileHandler(os.path.join(dirpath, "service.log"))
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(handler)
# 日志等级(从高到低):CRITICAL->ERROR->WARNING->INFO->DEBUG
# 一旦设置了日志等级,则调用比等级低的日志记录函数则不会输出
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):
import time
self.logger.error("svc do run....") # 记录一条错误日志
while self.isAlive:
self.logger.error("I am alive.")
time.sleep(1)
# 等待服务被停止
#win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
def SvcStop(self):
# 先告诉SCM停止这个过程
self.logger.error("svc do stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 设置事件
win32event.SetEvent(self.hWaitStop)
self.isAlive = False
if __name__=='__main__':
win32serviceutil.HandleCommandLine(PythonService)