以python2.7 為例
需要軟件
* python 2.7
* pywin32(與2.7 版本相匹配的) 安裝下載地址:http://sourceforge.net/projects/pywin32/files/pywin32/
Service Control Manager (SCM)
服務管理器(SCM) 是windows NT的 一部分,所有服務必須通過SCM 注冊,SCM負責啟動,停止服務等。
當一個進程通過SCM注冊后, 有如下特質:
* 運行該進程的用戶,未必是當前登錄的用戶。
* 該進程如果依賴其他服務,哪么該服務啟動前,依賴服務回啟動。該服務停止后,依賴服務會停止。(估計是應用計數減1)
* 服務可知計算機啟動后自動啟動,或者手動啟動。
windows NT 通過執行一個進程開始相應服務。一旦這個進程執行,它需要告知SCM它實際上是作為一個服務運行。還需要傳給SCM一個控制句柄(control handler)。其實就是一個函數,用於處理SCM 發來的相關信息。 當服務被停止時, SCM傳信息給控制句柄。服務本身負責處理該請求,並停止本身服務。
pywin32 服務相關module
* win32service 實現了Win32服務功能。
* win32serviceutil 對api的包裝,始面向用戶的接口更友好。
* PythonService.exe 使用pywin32 服務器,它必須先注冊。
下面重點講 win32serviceutil
服務框架類
win32serviceutil.ServiceFramework
__init__
構造函數,注冊ServiceCtrlHandler給SCM
ServiceCtrlHandler
本服務的control handler 的默認實現。該函數會查詢類內的函數名,用以判斷該服務提供哪些控制接口,比如類內有SvcPause 函數。則會認為該服務可以被暫停。
SvcRun
服務入口點。服務運行,就是運行這個函數。
# SmallestService.py # # A sample demonstrating the smallest possible service written in Python. import win32serviceutil import win32service import win32event class SmallestPythonService(win32serviceutil.ServiceFramework): _svc_name_ = "SmallestPythonService" _svc_display_name_ = "The smallest possible Python Service" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) # Create an event which we will use to wait on. # The "service stop" request will set this event. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): # Before we do anything, tell the SCM we are starting the stop process. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) # And set my event. win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): # We do nothing other than wait to be stopped! win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) if __name__=='__main__': win32serviceutil.HandleCommandLine(SmallestPythonService)
安裝服務
進入PythonService.exe所在目錄, 命令行下執行命令
(PATH)>PythonService.exe /register
Registering the Python Service Manager...
安裝服務
C:\Scripts> SmallestService.py install
Installing service SmallestPythonService to Python class
C:\Scripts\SmallestService.SmallestPythonService
Service installed
C:\Scripts>
啟動服務
C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
C:\Scripts>
啟動確認
C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
Error starting service: An instance of the service is already running.
C:\Scripts>
停止服務
C:\Scripts> python.exe SmallestService.py stop
Stopping service SmallestPythonService
C:\Scripts>
安裝服務
python PythonService.py install
讓服務自動啟動
python PythonService.py --startup auto install
啟動服務
python PythonService.py start
重啟服務
python PythonService.py restart
停止服務
python PythonService.py stop
刪除/卸載服務
python PythonService.py remove
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 = logging.getLogger('[PythonService]') this_file = inspect.getfile(inspect.currentframe()) dirpath = os.path.abspath(os.path.dirname(this_file)) handler = logging.FileHandler(os.path.join(dirpath, "service.log")) formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) 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)