Python Windows 服务 - 不响应内置 exe 的启动/停止(但在 python 中工作) [英] Python Windows Service - Not responding to start/stop from built exe (but works in python)

查看:92
本文介绍了Python Windows 服务 - 不响应内置 exe 的启动/停止(但在 python 中工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my first time building a windows service, and I thought I had it working. Installing as python aservice.py install works fine, and responds accordingly.

但是,由于我需要在其上安装此服务的机器没有安装 python,我想将其构建为可以安装该服务的可执行文件.虽然可执行文件成功安装服务,但当我尝试手动启动它时,或者通过 net startsc start 服务没有响应.

However, since the machines that I will need to install this service on, will not have python installed, I wanted to build it into an executable, that can install the service. Although the executable is successful in installing the service, When I try to start it either manually, or through net start or sc start The service does not respond.

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

Starting manually returns - Error 1053: The Service did not respond to the start or control request in a timely fashion.

Net Start 返回 - 服务未响应控制功能.

Net Start returns - The Service did not respond to the control function.

与python一起安装时,它响应所有命令,并且工作正常.不确定构建过程中发生了什么,但我显然遗漏了一些东西

When installed with python, it responds to all commands, and works fine. Not sure what is happening during the build process, but I am obviously missing something

使用 Python 3.4 64 位.所有需要该服务的盒子也将是 64 位的.

Using Python 3.4 64 bit. All boxes that need the service, will also be 64 bit.

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)

setup.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

build command = python setup.py py2exe

我也尝试过使用 Windows 安装 setup.py,效果相同,但不打印控制台日志.

I have tried setup.py with windows also, that works the same, but doesnt print console logs.

任何想法如何在没有 python 的计算机上正确安装此服务?

Any ideas how I can properly install this service on computers that dont have python?

setup.py 工作

setup.py working

推荐答案

`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' 
    ) `

这篇关于Python Windows 服务 - 不响应内置 exe 的启动/停止(但在 python 中工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆