Can't start Windows service written in Python (win32serviceutil)
Solution 1
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!
Solution 2
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)
Solution 3
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.
Solution 4
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.
Solution 5
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.
tas
Updated on July 16, 2022Comments
-
tas almost 2 years
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 locatepython27.dll
. It actually couldn't so I addedC:\Python27
toPATH
. Nowpythonservice.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.
-
BuvinJ almost 8 yearsIt might prove useful in this case to also add these directories to your system path:
C:\Python27\Lib\site-packages\win32
andC:\Python27\Lib\site-packages\pywin32_system32
. That will let you use pythonservice more easily. -
Kenneth about 7 years@BuvinJ This fixed my error 1053 while using PythonService.exe. thank you!
-
Thomas Ahle over 6 years@mpaf 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/…
-
user2171775 about 4 yearsThis 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.