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

8

20

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.

Beating answered 20/1, 2012 at 15:0 Comment(2)
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.Somaliland
+1 for mentioning that Error 1053 happens if pythonservice.exe can't locate python27.dll. Took me forever to figure this out! Thanks :)Buckler
D
16

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!

Deponent answered 29/1, 2013 at 10:25 Comment(3)
It might prove useful in this case to also add these directories to your system path: C:\Python27\Lib\site-packages\win32 and C:\Python27\Lib\site-packages\pywin32_system32. That will let you use pythonservice more easily.Ostracod
@Ostracod This fixed my error 1053 while using PythonService.exe. thank you!Radish
@Deponent Can you help describing how to add stuff to the System Path? Ah I see, you simply add it to the overall computer system path in the 'System Control Panel'. Personally I had to add anaconda as in snip2code.com/Snippet/770602/…Agna
C
6

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.

Cyclone answered 2/4, 2018 at 18:42 Comment(0)
B
4

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)
Barrios answered 20/4, 2012 at 19:13 Comment(0)
A
2

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.

Audwin answered 7/2, 2020 at 9:22 Comment(1)
This solution worked from me on python 3.8 , I tried other solutions such adding the win32 to the system PATH, also pywintypes38.dll to win32 but none of those works.Homophone
M
1

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.

Methedrine answered 27/7, 2016 at 16:3 Comment(0)
K
1

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 .

Kuhlman answered 21/10, 2020 at 11:44 Comment(0)
Q
0

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

Quinze answered 31/7, 2018 at 9:25 Comment(0)
A
0

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

Addict answered 5/11, 2019 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.