Can't start Windows service written in Python (win32serviceutil)

狂风中的少年 提交于 2019-11-28 05:28:36

Also, thanks for pointing out that it could be a DLL problem, that led me to find the right solution.

What you need to do is to add the Python27 to SYSTEM PATH, and not to USER PATH, since as a default the python service will get installed as a 'LocalSystem' and so when it attempts to start it uses the SYSTEM PATH variable - that's why you can run it from the command prompt, your USER PATH is right.

Hope it helps!

I believe your problem will be fixed by if you change method SvcDoRun

from

   def  SvcDoRun(self):
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

to

   def  SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

I also had this problem and was able to solve it by adding the following to my "__main__" execution block:

if len(sys.argv) == 1:
    servicemanager.Initialize()
    servicemanager.PrepareToHostSingle(RouterService)
    servicemanager.StartServiceCtrlDispatcher()
else:
    win32serviceutil.HandleCommandLine(RouterService)

(Don't forget to import servicemanager at the top of the file).

I believe the issue is that the Windows service manager runs the executable with no arguments (by default) and when this is the case, the application needs to properly be told to start the service, SvcDoRun is not called automatically it seems.

As others mentioned, you do need a path mapping if you run this from command line. In my application, I froze the service with cx_freeze and used the executable to install the service so all dependencies were included.

Another helpful tip is to add the following line

sys.frozen = 'windows_exe' # Fake py2exe so we can debug

before you call

win32serviceutil.HandleCommandLine(...)

That way you can get more helpful information from the service of what goes wrong.

I am able to run the service by following these procedure using Python 3.5 and PyInstaller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!