Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to start a simple service example:

someservice.py:

import win32serviceutil 
import win32service 
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "display service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

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

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)

When I run

python someservice.py install

everything is fine and the service appears in the Windows service list, but

python someservice.py start

fails with "Error 1053: The service did not respond to the start or control request in a timely fashion", but there is not any delay.

I googled a solution, which said it happens when pythonservice.exe can't locate python27.dll. It actually couldn't so I added C:\Python27 to PATH. Now pythonservice.exe runs fine, but Error 1053 still there.

I'm running Python 2.7.2 with pywin32 216 on Windows 7 Ultimate with admin privilegies.

like image 926
tas Avatar asked Jan 20 '12 15:01

tas


1 Answers

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!

like image 164
mpaf Avatar answered Sep 17 '22 00:09

mpaf