Python windows service “Error starting service: The service did not respond to the start or control request in a timely fashion”

pythonservice

I am running the code below by python win_service.py install from the normal command prompt, where I get access denied error.

Installing service TestService

Error installing service: Access is denied. (5)

which I was able to resolve when I started the command prompt by starting as administrator.

I was able to install the service, but I was unable to start the service.

Service installed

Starting service TestService

Error starting service: The service did not respond to the start or control request in a timely fashion.

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

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

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

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        print "running"

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

What am doing wrong, is there any other way to install the service that would solve the issue and how to dynamically run it as administrator.

Best Answer

I know this is old but I was stuck on this forever. For me, this specific problem was solved by copying this file - pywintypes36.dll

From -> Python36\Lib\site-packages\pywin32_system32

To -> Python36\Lib\site-packages\win32