我创建了一个.cmd
启动 Python 脚本的文件。我想通过将其添加为 Windows 服务
C:\Windows\System32>sc create DNSResolver127 binPath= "D:/bin/32dsdnsproxy.cmd"
[SC] CreateService SUCCESS
该服务在Services
面板中可见,但当我尝试启动它时,我在事件日志中遇到超时(2 个事件):
A timeout was reached (30000 milliseconds) while waiting for the DNSResolver127 service to connect.
The DNSResolver127 service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion.
尽管有 30 秒超时,但在尝试启动服务时错误会立即出现。
它从CMD
提示开始没有问题。
.cmd
作为服务启动时对文件有特殊要求吗?
作为 Windows 服务启动时,对所有内容都有特殊要求。
Windows 中的服务管理器为每个服务创建了一个特殊的控制通道,通过它发送停止/暂停/恢复命令,服务本身必须通过它通知管理器它已“准备就绪”。如果您的 Python 脚本没有这样做,服务管理器会认为它无法启动并且将被终止。
在用 Python 编写服务时,您可以使用
win32service
PyWin32 中的相关模块:注意:您不需要.cmd 脚本来启动 Python 脚本——binPath 可以直接引用 Python 解释器。