无法启动用 Python 编写的 Windows 服务(win32serviceutil) [英] Can't start Windows service written in Python (win32serviceutil)

查看:35
本文介绍了无法启动用 Python 编写的 Windows 服务(win32serviceutil)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动一个简单的服务示例:

I'm trying to start a simple service example:

someservice.py:

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)

当我跑步时

python someservice.py install

一切正常,服务出现在Windows服务列表中,但是

everything is fine and the service appears in the Windows service list, but

python someservice.py start

失败并显示错误 1053:服务没有及时响应启动或控制请求",但没有任何延迟.

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.

我在谷歌上搜索了一个解决方案,它说当 pythonservice.exe 找不到 python27.dll 时会发生这种情况.它实际上不能,所以我将 C:\Python27 添加到 PATH.现在 pythonservice.exe 运行良好,但错误 1053 仍然存在.

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.

我在 Windows 7 Ultimate 上以管理员权限运行 Python 2.7.2 和 pywin32 216.

I'm running Python 2.7.2 with pywin32 216 on Windows 7 Ultimate with admin privilegies.

推荐答案

另外,感谢您指出这可能是 DLL 问题,这让我找到了正确的解决方案.

Also, thanks for pointing out that it could be a DLL problem, that led me to find the right solution.

您需要做的是将 Python27 添加到系统路径,而不是用户路径,因为默认情况下,python 服务将安装为本地系统",因此当它尝试启动时,它使用系统路径变量 - 这就是您可以从命令提示符运行它的原因,您的用户路径是正确的.

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.

希望有帮助!

这篇关于无法启动用 Python 编写的 Windows 服务(win32serviceutil)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆