开发者社区> 问答> 正文

python写的windows服务,启动显示错误:服务没有及时响应启动或控制请求

'''python

import win32service
import win32serviceutil
import win32event

import os, time

import winerror

class service(win32serviceutil.ServiceFramework):

_svc_name_ = 'service'
_svc_display_name_ = 'service'
_svc_description_ = 'service'

def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self, args)
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    self.isAlive = True

def SvcDoRun(self):
    while self.isAlive:
        print('do something')
        time.sleep(2)
    self.ReportServiceStatus(win32service.SERVICE_RUNNING)
    win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop)
    self.isAlive = False

if name == '__main__':

import sys
import servicemanager
if len(sys.argv) == 1:
    try:
        evtsrc_dll = os.path.abspath(servicemanager.__file__)
        servicemanager.PrepareToHostSingle(service)
        servicemanager.Initialize('service', evtsrc_dll)
        servicemanager.StartServiceCtrlDispatcher()
    except win32service.error as details:
        import winerror
        if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
            win32serviceutil.usage()
else:
    win32serviceutil.HandleCommandLine(service)

'''
服务能安装,但是启动就显示如题的错误,代码我也debug过,能输入run里面那句话。然后我查看windows事件查看器,有两条错误启动的记录。
_
_
另一条记录:
_
_

弄了好久怎么也搞不定。特来请教各位

展开
收起
concealment 2017-10-29 15:11:29 9869 0
1 条回答
写回答
取消 提交回答
  • 个人也遇到这个问题,
    先确定服务:可执行文件的路径:
    "xxxxxPython36libsite-packageswin32PythonService.exe"
    PythonService.exe是否能运行,若能说明没有xxx.dll动态库问题。

    配置python path不能设置在user下,应该设置在system path下就可以了,参考下面的说明:
    https://stackoverflow.com/questions/8943371/cant-start-windows-service-written-in-python-win32serviceutil

    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.

    个人的这个问题是这样解决的。

    2019-07-17 21:41:31
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
ECS运维指南之Windows系统诊断 立即下载
Data Pre-Processing in Python: 立即下载