2

Alright so i have a pretty solid idea of how to make a Windows service in python. However my endgame is to compile my script to exe and have the service install itself once the executable is clicked. Usually when you make a service you write it, and at the end include a call to the service handler like so:

def ctrlHandler(ctrlType):
   return True

if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)
   win32serviceutil.HandleCommandLine(aservice)`

Then you can install it from the command line, I need some logic however to automate this process.

scytale
  • 12,346
  • 3
  • 32
  • 46
NullCoder
  • 21
  • 5
  • I use pyinstaller actually but i have py2exe as well. However the problem is not manually installing the service after it's compiled. The problem is that i want the executable to install the service without user interference. If i compile with py2exe i'll still have to manually install the service from the command line with the command `service.exe -install` and then `net start "service name"`. I need this process to transpire automatically. – NullCoder Aug 24 '15 at 16:53
  • Indeed, any relevant code examples or documentation to get me started? Found this on calling the control dispatcher http://stackoverflow.com/a/25934756/5260027 – NullCoder Aug 24 '15 at 19:25
  • For the most part you should be able to pass a custom `argv` to PyWin32's [`HandleCommandLine`](http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/file/713ac73c22e5/win32/Lib/win32serviceutil.py#l525). To detect if you're a service, call [`win32service.EnumServicesStatusEx`](http://docs.activestate.com/activepython/2.7/pywin32/win32service__EnumServicesStatusEx_meth.html) to check for your PID in the list of running services. – Eryk Sun Aug 24 '15 at 19:36
  • Thank you i'm going to look into that. – NullCoder Aug 24 '15 at 20:03

0 Answers0