mport win32service
import win32serviceutil
import time
class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "aservice - It Does nothing"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.isAlive = True
def SvcDoRun(self):
import servicemanager
while self.isAlive:
servicemanager.LogInfoMsg("aservice - is alive and well")
time.sleep(10)
servicemanager.LogInfoMsg("aservice - Stopped")
def SvcStop(self):
import servicemanager
servicemanager.LogInfoMsg("aservice - Recieved stop signal")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.isAlive = False
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(aservice)
上面的代码SvcDoRun,SvcStop 没有缩进,因此aservice中就相当于没有声明这两个方法,在启动服务时就会报
该服务器已经启动,并且关闭。服务不能正常启动。
详细的日志:
The instance's SvcRun() method failed
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\win32\lib\win32serviceutil.py", line 806, in SvcRun
self.SvcDoRun()
AttributeError: aservice instance has no attribute 'SvcDoRun'
结论:拷贝黏贴害死人。