Python Windows Service - Not responding to start/stop from built exe (but works in python)

╄→гoц情女王★ 提交于 2019-12-10 12:04:38

问题


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.

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.

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

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

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

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

Service Definition

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

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

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

Edit: setup.py working


回答1:


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


来源:https://stackoverflow.com/questions/32142907/python-windows-service-not-responding-to-start-stop-from-built-exe-but-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!