19
votes

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.

8
This worked for me, running Python 2.6.6 (64 bit) and pywin32 216 amd64-py26. I realize that's not very helpful though. On Windows 7 Pro.jgritty
+1 for mentioning that Error 1053 happens if pythonservice.exe can't locate python27.dll. Took me forever to figure this out! Thanks :)Gili

8 Answers

15
votes

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!

3
votes

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.

3
votes

I believe your problem will be fixed by changing the 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)
2
votes

Make sure you run the application with a different user than the default Local System user. Replace it with the user you successfully be able to run the debug command with.

To replace the user go to the windows services (start > services.msc) Right click on the service you created > properties > Log On Uncheck the Local System Account and enter your own. from all python windows service can not start{error 1053} worked for me.

Because I just set PATH for user, It missing environment when run at Local System Account.

1
votes

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.

1
votes

I was unable to start python services too , I have python39 installed.

my problem was a missing pywintypes39.dll and this resulted to error 1053 could not start service

the module responsible for starting python services in windows is found on the path C:\Users\.....\AppData\Local\Programs\Python\Python39\Lib\site-packages\win32 pythonservice.exe

but the missing .dll are installed in a subfolder called lib \AppData\Local\Programs\Python\Python39\Lib\site-packages\win32\lib

transferring missing .dlls from \Lib folder to root folder \win32 where pythonservice.exe will make the accessible to the service program.

this worked for me .

0
votes

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

0
votes

The cause of errors may come from different reasons. Besides using a log flow, I strongly recommand considering nssm: http://nssm.cc/download

From my experience; I struggled a lot to make windows services from python files, batch files (.bat) and executables. Nssm is an excellent tool to install your services with, unlike sc or from raw python code with win32serviceutil.

Have a module that calls the main function and run the module inside a batch file. Then the batch file can be used for the windows service.

In the python module:

""" 
main.py 
""" 
from myCode import run.py

run()

In service.bat:

python main.py

And then using nssm you can easily install a windows service pointing to the file service.bat