0

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

Busturdust
  • 2,447
  • 24
  • 41
  • do you have extra files in your application? if `yes` : add `import sys,os;sys.path.append(os.getcwd()+'/')` – dsgdfg Aug 21 '15 at 14:43
  • @SDilmac no extra files, but I am importing a custom module. I am pretty sure this is related to the py2exe, I am going through the docs right now hoping to find something – Busturdust Aug 21 '15 at 14:50
  • Your application(exe) work fine? – dsgdfg Aug 21 '15 at 14:51
  • The Exe works fine as far as `Logon_Service.exe install` However `Logon_Service.exe start` Does not – Busturdust Aug 21 '15 at 14:53
  • @SDilmac I Found http://www.py2exe.org/index.cgi/py2exeAndWindowsServices I am trying to edit for my uses, no luck yet – Busturdust Aug 21 '15 at 14:54
  • try (on root cmd): `sc create newservice binpath= c:\your_app_path` without any one additional arguments. and check `sc` documents – dsgdfg Aug 21 '15 at 14:59
  • @SDilmac Again, it is successfull in installing the service, but it doesnt respond to any requests (start) – Busturdust Aug 21 '15 at 15:02
  • try (on root cmd) : `service your_exe_name start` – dsgdfg Aug 21 '15 at 15:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87599/discussion-between-sdilmac-and-busturdust). – dsgdfg Aug 21 '15 at 15:38
  • I don't think the logic of `if __name__ == '__main__':` works with py2exe, so you should make the lines following it unconditional. – martineau Aug 21 '15 at 16:02
  • The instructions for a service are in [docs/py2exe.txt](http://sourceforge.net/p/py2exe/svn/HEAD/tree/trunk/py2exe/docs/py2exe.txt#l277). The service setup uses the [boot_service](http://sourceforge.net/p/py2exe/svn/HEAD/tree/trunk/py2exe/py2exe/boot_service.py) script, which handles the command line options and/or calls `servicemanager.StartServiceCtrlDispatcher()`. – Eryk Sun Aug 21 '15 at 17:07

2 Answers2

0

Make sure the correct pywintypes{version}.dll is located in your C:/Windows/System 32/ folder.

Alec Thomas
  • 176
  • 1
  • 2
  • 12
-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' 
    ) `
Busturdust
  • 2,447
  • 24
  • 41