Python Windows服务-不响应从构建的exe启动/停止(但在Python中工作)



这是我第一次构建windows服务,我以为我已经可以运行了。作为python aservice.py install安装工作正常,并相应地响应。

然而,因为我需要安装这个服务的机器没有安装python,我想把它构建成一个可执行文件,可以安装这个服务。虽然可执行文件成功安装服务,但当我尝试手动启动或通过net startsc start启动服务时,服务没有响应。

手动启动返回-错误1053:服务没有及时响应启动或控制请求。

Net Start返回-服务没有响应控制函数。

当使用python安装时,它会响应所有命令,并且工作正常。不确定在构建过程中发生了什么,但我显然错过了一些东西

使用Python 3.4 64位。所有需要这项服务的机顶盒也将是64位的。

服务定义
class aservice(win32serviceutil.ServiceFramework):
   _svc_name_ = "Test Login Service"
   _svc_display_name_ = "Test Login Service"
   _svc_description_ = "Test"
   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)
           win32event.SetEvent(self.hWaitStop)                    
   def SvcDoRun(self):
      import servicemanager      
      servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, '')) 
      self.timeout = 3000
      while 1:
         # Wait for service stop signal, if I timeout, loop again
         rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
         # Check to see if self.hWaitStop happened
         if rc == win32event.WAIT_OBJECT_0:
            # Stop signal encountered
            servicemanager.LogInfoMsg("aservice - STOPPED")
            break
         else:
            servicemanager.LogInfoMsg("aservice - is alive and well")
             ...Doing Things...
            servicemanager.LogInfoMsg("Logon Service Has Completed, Stopping")
            time.sleep(10)   
            break
def ctrlHandler(ctrlType):
   return True
if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
   win32serviceutil.HandleCommandLine(aservice)
py

`from distutils.core import setup 
import py2exe 

# setup.py 
# 
class Target: 
    def __init__(self, **kw): 
        self.__dict__.update(kw) 
        # for the versioninfo resources 
        self.version = "0.5.0" 
        self.company_name = "Company" 
        self.copyright = "no copyright" 
        self.name = "Test22" 

myservice = Target( 
    description = 'Edit Logon Service', 
    modules = ['Logon_Service'], 
    cmdline_style='pywin32' 
    ) `

build command = python setup.py py2exe

我也尝试了windows的setup.py,工作原理相同,但不打印控制台日志。

你知道我怎样才能在没有python的电脑上正确安装这个服务吗?

编辑:setup.py working

确保正确的pywintypes{version}.dll位于您的C:/Windows/System 32/文件夹

`from distutils.core import setup 
import py2exe 

# setup.py 
# 
class Target: 
    def __init__(self, **kw): 
        self.__dict__.update(kw) 
        # for the versioninfo resources 
        self.version = "0.5.0" 
        self.company_name = "Company" 
        self.copyright = "no copyright" 
        self.name = "Test22" 

myservice = Target( 
    description = 'Edit Logon Service', 
    modules = ['Logon_Service'], 
    cmdline_style='pywin32' 
    ) `

最新更新