pythonwindows-servicesmod-wsgiwsgicherrypy

Installing and Starting a CherryPy server script as Windows service (using sc.exe)


I am trying to install and start a simple CherryPy server as a Windows service.

Here is the script: (Removed some lines to cut it short. It's fully working when executing manually from the command-line)

app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler())
logged_app = TransLogger(app)
server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8632), logged_app, server_name='localhost', numthreads=20 )

try:
    server.start()
except KeyboardInterrupt:
    server.stop()

I'm using sc.exe to install and start the service. Installation goes fine, but I can't seem to start the service.

The command used is: (note there're spaces in the paths, though I'm handeling this with double-quotes, and the binPath is working when executing its string manually through the command-line)

> sc.exe create "ServiceName" binPath= "\"C:\Path to Python\python.exe\" \"C:\Path to CherryPy Script\cherryserver.py\""

> sc.exe start "ServiceName"

I keep getting this error, no matter if attempting to start the service using sc.exe or through services.msc GUI:

[SC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.

From what I understand, this is happenning because python.exe doesn't implement the Windows Service API.

I do not wish to create an .exe from the script, using py2exe.

I have found this answer that suggests to install the service using different tools than sc.exe, called srvany.exe & instsrv.exe. However, I can't find them in the Win2K Resource Kit website.

Does anybody know how to install & start this .py as a Windows succesfully?

Does anybody know


Solution

  • I eventually used ServiceInstaller aka SMaster, as stated in this answer. The URL in the given answer is broken, and I couldn't find a working URL. I just had srunner.exe available locally beforehand.

    Note there was another obstacle to overcome though, as ServiceInstaller can't handle files with spaces in their paths.

    So, I used the old DOS path formatting for service registration.

    Instead of registering C:\Program Files\MyApp\python.exe, I registered C:\PROGRA~1\MyApp\python.exe.