【发布时间】:2018-05-29 18:32:01
【问题描述】:
我已经检查了至少几十个与我类似的案例,但仍然没有想出解决方案,我希望有人能解释一下,这里一定是我遗漏了一些东西。
我正在使用 Python3.6 制作 Windows 服务,如果服务没有运行,则必须运行 .exe 文件。这是.py:
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import psutil
import subprocess
import os, sys, string, time
import servicemanager
class SLAAgent (win32serviceutil.ServiceFramework):
_svc_name_ = "SLAAgent"
_svc_display_name_ = "SLAAgent"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
self.isAlive = True
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.isAlive = False
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self._logger.info("Service Is Starting")
main(self)
def main(self):
while self.isAlive:
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
servicemanager.LogInfoMsg("SLAAService has stopped") #For Event Log
break
else:
try:
s = subprocess.check_output('tasklist', shell=True)
if "SLA_Client.exe" in s:
pass
else:
pass
#execfile("SLA_Client.exe") #Execute the script
except:
pass
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(SLAAgent)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(SLAAgent)
我已经安装了 pywin32 包,将它们添加到 PATH 中,因为它在几个解决方案中都被建议,并且还将 .dll 从 pywin32_system32 复制到 win32
事件查看器在我每次运行 python service.py 或 python service.py start 时都会打印此错误,控制台也会打印此错误:
python SLA_Agent.py
Traceback (most recent call last):
File "SLA_Agent.py", line 56, in <module>
servicemanager.StartServiceCtrlDispatcher()
pywintypes.error: (1063, 'StartServiceCtrlDispatcher', 'The service process
could not connect to the service controller.')
当尝试从服务工具启动服务时,会出现此错误。我也看到了另一个错误,关于服务没有及时响应。
我试过用pyinstaller和nuitka编译,错误是一样的。我不确定如何继续,我已经更改了代码以适应我使用 google 和 SO 找到的示例和解决方案,并且对方法和原因知之甚少。
如果有人以前遇到过这些问题,我非常感谢您的意见,到目前为止,其他答案对我没有帮助。
后期编辑:修复代码缩进
【问题讨论】:
-
我假设
if __name__ == '__main__':下的代码应该缩进。 -
使用
pyinstaller创建.exe-- 然后使用my_service.exe debug运行该服务并查看它在您的控制台中执行。如果可行,请执行my_service.exe install安装该服务。您应该在服务列表中看到它。然后my_service.exe start启动它(或使用服务 gui)。很确定它不相关,但我需要做的一件常见事情是将 win32timezone 指定为 pyinstaller 的隐藏导入。 -
@sytech 我会尝试并很快通知您,但这几乎是我在 MartinEvans 之前所做的事情,它应该缩进到类 SLAAgent 的同一级别,否则 PrepareToHostSingle 将抱怨未定义 SLAAgent。
-
我相信你已经看过这个,因为代码看起来很像,但我使用这个ryrobes.com/python/running-python-scripts-as-a-windows-service 几乎是为了在 Windows 7 和 python2 中成功运行服务。另外,可能要添加
pywin32标签。我敢肯定人们有时会搜索该标签以尝试回答问题。 -
@DanielF。是的,我确实找到了该代码并在此基础上构建了一些代码。我一定是做错了什么,今天我有机会在另一台没有安装 Python 的计算机上尝试该服务
标签: python windows service pyinstaller pywin32