问题
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.
回答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!
回答2:
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:
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)
回答4:
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.
回答5:
I am able to run the service by following these procedure using Python 3.5 and PyInstaller
回答6:
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
来源:https://stackoverflow.com/questions/8943371/cant-start-windows-service-written-in-python-win32serviceutil