pywin32 service start error: no module named 'tmp'

pywin32 service start error: no module named 'tmp'

问题

我正在尝试使用 pywin32 将 Python 脚本作为 Windows 服务启动。我可以正确地安装和删除该服务,但是在安装时,它的状态永远不会改变 "stopped." 我如何修复我的代码以便该服务实际上 运行s?

代码

#!/bin/python

import winerror
import win32event
import win32service
import win32serviceutil

SVC_RUN = 1
SVC_REMOVE = 2

class TMP_Service(win32serviceutil.ServiceFramework):

    _svc_name_ = "tmp_svc_name"
    _svc_display_name_ =  "tmp svc disp name"
    _svc_reg_class = "tmp.reg_class"
    _svc_description_ = "tmp description"

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

    def SvcStop(self):
        self.reportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # I will call reactor.callFromThread(reactor.stop) here to stop the 
        # FTP and SCP listeners
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)

        # This infinite loop simulates future behavior of my service. It will
        # run a Twisted reactor to handle FTP and TCP network connections.
        while True:
            pass 

        win32event.WaitforSingleObject(self.hWaitStop, win32event.INFINITE)

def install_svc():

    try:
        win32serviceutil.InstallService(
                TMP_Service._svc_reg_class,
                TMP_Service._svc_name_,
                TMP_Service._svc_display_name_,
                startType=win32service.SERVICE_AUTO_START)

    except win32service.error as e:

        if e[0] == winerror.ERROR_SERVICE_EXISTS:
            pass # ignore install error if service is already installed

        else:
            raise

def handle_service(svc):

    if svc == SVC_RUN:

        try:
            win32serviceutil.StartService(TMP_Service._svc_name_)

        except win32service.error as e:

            if ((e[0] == winerror.ERROR_SERVICE_ALREADY_RUNNING) or
                    (e[0] == winerror.ERROR_ALREADY_RUNNING_LKG)):
                pass # ignore failed start if the service is already running

            elif e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:

                # if the service is not installed, install it and try again
                install_svc()
                win32serviceutil.StartService(TMP_Service._svc_name_)

            else:
                # reraise any other start expection
                raise

        status = win32serviceutil.QueryServiceStatus(TMP_Service._svc_name_)
        print("Service status: {}".format(status[1]))

    else:

        try:
            win32serviceutil.RemoveService(TMP_Service._svc_name_)

        except win32service.error as e:

            if e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
                pass # ignore failed removal if service is not installed
            else:
                # reraise any other remove exception
                raise

if __name__ == "__main__":

    handle_service(SVC_RUN)

其他详情

  1. 当我查看系统事件日志时,我看到了这个错误:

    Python could not import the service's module
    ImportError: No module named tmp
    %2: %3

    这些消息的时间戳与我尝试运行此脚本的时间相匹配。

  2. 我看到了这个问题:Can't start Windows service written in Python (win32serviceutil)。根据那里的建议,我使我的代码与那里顶部答案中的建议相匹配,并确保 C:\Python27 在我的系统路径中。这两个建议都没有影响。

  3. 打印的状态总是“2”。根据MSDN,这是SERVICE_START_PENDING,这意味着服务应该正在启动。

更新详情

  1. 如果我将 _svc_reg_class_ 属性的值更改为 "tmp2.reg_class",那么缺少模块的名称,如 Windows 事件日志中所报告的那样,更改为 "tmp2",因此此错误与 _svc_reg_class_ 属性有关。

  2. 回复下面的评论:我不认为我可以捕获完整的回溯,因为我的服务 class 是在 pywin32 库代码中实例化和使用的。有问题的导入不会在我的代码中的任何地方发生,所以我没有什么可以包装在 try/except 块中。

更改 _svc_reg_class = "tmp.reg_class" 如下所示。

try:
    module_path = modules[TMP_Service.__module__].__file__
except AttributeError:
    # maybe py2exe went by
    from sys import executable
    module_path = executable

module_file = splitext(abspath(module_path))[0]
TMP_Service._svc_reg_class = '%s.%s' % (module_file, TMP_Service.__name__)

以下是您原始代码的完整修改版本。

#!/bin/python

import winerror
import win32event
import win32service
import win32serviceutil
from sys import modules
from os.path import splitext, abspath

SVC_RUN = 1
SVC_REMOVE = 2

class TMP_Service(win32serviceutil.ServiceFramework):

    _svc_name_ = "tmp_svc_name"
    _svc_display_name_ =  "tmp svc disp name"
    _svc_reg_class = "tmp.reg_class"
    _svc_description_ = "tmp description"

    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)
        self.Stop = True
        # I will call reactor.callFromThread(reactor.stop) here to stop the 
        # FTP and SCP listeners
        win32event.SetEvent(self.hWaitStop)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def SvcDoRun(self):
        self.Stop = False
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)

        # This infinite loop simulates future behavior of my service. It will
        # run a Twisted reactor to handle FTP and TCP network connections.
        while self.Stop == False:
            pass 

        win32event.WaitforSingleObject(self.hWaitStop, win32event.INFINITE)

def install_svc():

    try:
        module_path = modules[TMP_Service.__module__].__file__
    except AttributeError:
        # maybe py2exe went by
        from sys import executable
        module_path = executable

    module_file = splitext(abspath(module_path))[0]
    TMP_Service._svc_reg_class = '%s.%s' % (module_file, TMP_Service.__name__)

    try:
        win32serviceutil.InstallService(
                TMP_Service._svc_reg_class,
                TMP_Service._svc_name_,
                TMP_Service._svc_display_name_,
                startType=win32service.SERVICE_AUTO_START)

    except win32service.error as e:

        if e[0] == winerror.ERROR_SERVICE_EXISTS:
            pass # ignore install error if service is already installed

        else:
            raise

def handle_service(svc):

    if svc == SVC_RUN:

        try:
            win32serviceutil.StartService(TMP_Service._svc_name_)

        except win32service.error as e:

            if ((e[0] == winerror.ERROR_SERVICE_ALREADY_RUNNING) or
                    (e[0] == winerror.ERROR_ALREADY_RUNNING_LKG)):
                pass # ignore failed start if the service is already running

            elif e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:

                # if the service is not installed, install it and try again
                install_svc()
                win32serviceutil.StartService(TMP_Service._svc_name_)

            else:
                # reraise any other start expection
                raise

        status = win32serviceutil.QueryServiceStatus(TMP_Service._svc_name_)
        print("Service status: {}".format(status[1]))

    else:

        try:
            win32serviceutil.RemoveService(TMP_Service._svc_name_)

        except win32service.error as e:

            if e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
                pass # ignore failed removal if service is not installed
            else:
                # reraise any other remove exception
                raise

if __name__ == "__main__":
    handle_service(SVC_RUN)

参考:here