Ory Band
Ory Band

Reputation: 15804

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

Upvotes: 0

Views: 4379

Answers (3)

fumanchu
fumanchu

Reputation: 14559

CherryPy ships with a module for starting as a Windows service. See this other SO question for instructions on how to install and run it. You'll probably want to switch from your current approach (of passing the Django app directly to the WSGIServer) and use cherrypy.tree.graft instead.

Upvotes: 5

SlashGordon
SlashGordon

Reputation: 880

I prefer nssm for installing normal scripts as a service. You can copy the nssm.exe in the C:\Windows\system32 or C:\Windows\SysWOW64 directory depending on your system. After that you are able to install a Service as follow:

nssm install yourservicename

For a python script you must set the application path to your python.exe and the argument is your script self.

Basic configuration for nssm

Other common commands for start/stop/edit your service are:

nssm start yourservicename
nssm stop yourservicename
nssm edit yourservicename

Upvotes: 2

Ory Band
Ory Band

Reputation: 15804

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.

Upvotes: 0

Related Questions