13

I am writing a python win32 service below are snippet of my code when i compile the service it works but i need to go to services.msc and start it manually.

Is there an option when i install the serivce by : myservice.exe install it will starts automaticly ?

below are snippet of my code :

import win32serviceutil
import win32service
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
   _svc_name_ = "ser_name"
   _svc_display_name_ = "ser_descryption"
   #_svc_description_='ddd'
   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):

       win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':

    win32serviceutil.HandleCommandLine(SmallestPythonService)
flam3
  • 1,897
  • 2
  • 19
  • 26
AKM
  • 6,285
  • 9
  • 31
  • 34

4 Answers4

18

Use myservice.exe --startup=auto install to install the service and set it to be started automatically.

Maciejg
  • 3,088
  • 1
  • 17
  • 30
2

I would take a look at this ActiveState recipe. It's a wrapper around the win32serviceutil that shows how to auto-start the service.

Mark
  • 106,305
  • 20
  • 172
  • 230
1

You can use sc.exe with the create command.

sc create MyPyService binPath= "C:\myservice.exe" DisplayName= "Some Python Service"

More on this at Microsoft KB251192.

win32serviceutil also has a InstallService() function you might be able to use.

kichik
  • 33,220
  • 7
  • 94
  • 114
  • when i start the service using `sc start MyPyService` i get the error. `[SC] StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion.` – programmer Oct 08 '21 at 05:28
-1

@Maciejg doesn't work for me, here the solution to start automaticaly my service builded with py2exe :

myservice.exe -auto -install
Guillaume Vincent
  • 13,355
  • 13
  • 76
  • 103