Windows 当 运行 通过命令行而不是通过服务时服务工作

Windows service works when running through command line but not through Services

this guide 之后,我从 python 脚本创建了一个服务并使用了 "py [script name] install"。该服务已成功安装,我可以在服务中看到它window。如果我使用 "py [script name] start" 通过命令行 运行 服务,一切正常。

但是,当我尝试从 Windows 服务 window 启动它时,它会根据我使用的帐户给出不同的错误。所有这些都是我在各种论坛上看到的建议:

1) 默认(本地系统帐户)

当我尝试这个时,出现错误 "Windows could not start the service on Local Computer."

2) 管理员帐户,我运行从

中使用 cmd

同上错误

3) 本地服务

即使我以管理员身份登录,也会出现 "Error 5: Access is denied."。

有人知道是什么原因造成的吗?

我还尝试从 python 脚本创建一个 .exe,并使用 sc.exe 将其添加为服务。但是,当我这样做时,我从来没有超过 "The service did not respond to the start or control request in a timely fashion." 当我从 CMD 运行 将它复制到 python 安装目录中时,也会发生此错误。

我从未尝试过 python 脚本即服务,所以我不能直接说。

我知道 "The service did not respond to the start or control request in a timely fashion." 错误几乎总是表明 OnStart() 回调到该服务(在您的情况下看起来像 SvcDoRun)开始一个从不 return 的长 运行 操作(例如,while 循环)。 OnStart() 回调预计将启动一个前台线程,该线程将保持服务 运行 然后 return。根据我的回忆,如果 OnStart() 在大约 30 秒左右的时间内没有 return,Windows 会终止服务无响应,从而触发此特定错误消息。

另一件要看的事情是事件查看器。由于进程隔离,您的服务可能抛出了您没有看到的异常。