分类: Python/Ruby
2018-07-06 15:24:15
'''python
import win32service
import win32serviceutil
import win32event
import os, time
import winerror
class service(win32serviceutil.ServiceFramework):
_svc_name_ = 'service' _svc_display_name_ = 'service' _svc_description_ = 'service' def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.isAlive = True
def SvcDoRun(self): while self.isAlive: print('do something') time.sleep(2) self.ReportServiceStatus(win32service.SERVICE_RUNNING) win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) self.isAlive = False
if name == '__main__':
import sys import servicemanager if len(sys.argv) == 1: try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(service)
servicemanager.Initialize('service', evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error as details: import winerror if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage() else:
win32serviceutil.HandleCommandLine(service)
'''
服务能安装,但是启动就显示如题的错误,代码我也debug过,能输入run里面那句话。然后我查看windows事件查看器,有两条错误启动的记录。