安装和启动CherryPy服务器脚本作为Windows服务(使用sc.exe)

时间:2011-05-19 15:40:04

标签: python windows-services mod-wsgi wsgi cherrypy

我正在尝试安装并启动一个简单的 CherryPy 服务器作为Windows服务。

这是脚本:(删除了一些行以缩短它。当从命令行手动执行时它完全正常工作)

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()

我正在使用sc.exe来安装和启动该服务。安装顺利,但我似乎无法启动服务。

使用的命令是:(注意路径中有空格,虽然我用双引号处理它,并且binPath在通过命令行手动执行其字符串时正在工作)< / p>

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

> sc.exe start "ServiceName"

无论是尝试使用sc.exe还是通过services.msc GUI启动服务,我都会收到此错误:

[SC] StartService FAILED 1053:

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

根据我的理解,这是因为python.exe没有实现Windows服务API。

我不希望使用 py2exe 从脚本中创建.exe

我发现this answer建议使用与sc.exe不同的工具安装服务,名为srvany.exe&amp; instsrv.exe。但是,我无法在Win2K Resource Kit website中找到它们。

有人知道如何安装&amp;成功启动此.py作为Windows?

有人知道吗

3 个答案:

答案 0 :(得分:5)

CherryPy附带a module for starting as a Windows service。有关如何安装和运行它的说明,请参阅this other SO question。您可能希望从当前的方法(将Django应用程序直接传递给WSGIServer)切换,然后使用cherrypy.tree.graft

答案 1 :(得分:1)

我更喜欢nssm将普通脚本安装为服务。 您可以根据您的系统复制 C:\ Windows \ system32 C:\ Windows \ SysWOW64 目录中的 nssm.exe 。之后,您可以按如下方式安装服务:

nssm install yourservicename

对于python脚本,您必须设置 python.exe 的应用程序路径,并且参数是您自己的脚本。

Basic configuration for nssm

启动/停止/编辑服务的其他常用命令是:

nssm start yourservicename
nssm stop yourservicename
nssm edit yourservicename

答案 2 :(得分:0)

我最终将 ServiceInstaller 又名 SMaster 用作stated in this answer。给定答案中的URL已损坏,我找不到有效的URL。我事先在本地提供srunner.exe

请注意,还有另一个障碍需要克服,因为 ServiceInstaller 无法处理路径中带有空格的文件

因此,我使用旧的DOS路径格式进行服务注册。

我没有注册C:\Program Files\MyApp\python.exe,而是注册了C:\PROGRA~1\MyApp\python.exe

相关问题