Python Win32serviceutil Install Service

  1. For Python you can do this, which creates the service in one go: nssm install MyServiceName c: python27 python.exe c: temp myscript.py. Where myscript.py is the boilerplate script above, modified to invoke your application script or code in the main function.
  2. The following example shows how to start a service, stop a service, restart a service, or get service status through Python: import win32serviceutil, time def serviceinfo(action, machine, service).

I used this script to run a conversion service with Python on Windows. I stripped it down, so that others can use it for their needs. Install the script from the windows shell: python WindowsService.py install. And start it by: python WindowsService.py start. Import win32serviceutil. Import win32evtlogutil. python Scripts/pywin32postinstall.py -install ## Building from source Building from source has been simplified recently - you just need Visual Studio and the Windows 10 SDK installed (the free compilers probably work too, but haven’t been tested - let me know your experiences!). As is common with Python extensions, there are two Python modules that can work with services: The win32service module implements the Win32 service functions, while the win32serviceutil module provides some handy utilities that utilize the raw API.

2009-01-17 01:35:50 UTC
Hi, I've implemented the MyService.py example from the py2exe samplesInstall
directory using Python2.6, on my Windows XP SP2, and I get the error
1052...did not respond in a timely fashion immediately when I run the
MyService.py, or the MyService.exe, or if I install it as a service using an
NSIS installer script I wrote, and start it using the windows service
manager.
Any ideas? Here is the code reduced to just the essentials:
import win32serviceutil
import win32service
import win32event
import win32evtlogutil
class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'Win32serviceutil

Installutil

_svc_deps_ = ['EventLog']
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):
import servicemanager
log('running')

Python Win32serviceutil Install Service Pack

win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
log('stopped')
if __name__ '__main__':
win32serviceutil.HandleCommandLine(MyService)
I've seen other posters with similar problems, but with theirs it doesn't
work in the .exe, but in my case it doesn't work when I run the .py either.
python MyService.py
Collecting Python Trace Output...
Traceback (most recent call last):
File 'C:Python26Libsite-packageswin32libwin32serviceutil.py', line
399,
in StartService

Python Win32serviceutil

win32service.StartService(hs, args)
pywintypes.error: (1053, 'StartService', 'The service did not respond to the
sta
rt or control request in a timely fashion.')

Python Win32serviceutil Install Service Command

Thanks in advance,
Richard

Python Win32serviceutil Install Service Panel