Python Win32serviceutil Install Service

Nssm is an excellent tool to install your services with, unlike sc or from raw python code with win32serviceutil. Have a module that calls the main function and run the module inside a batch file. Then the batch file can be used for the windows service.
service.py
| importwin32serviceutil |
| importwin32service |
| importwin32event |
| importservicemanager |
| # import signal |
| # import os |
| frommultiprocessingimportProcess |
| frommainimportapp |
| # Usage: |
| # python service.py install |
| # python service.py start |
| # python service.py stop |
| # python service.py remove |
| # python service.py --username <username> --password <password> --startup auto install |
| classService(win32serviceutil.ServiceFramework): |
| _svc_name_='ServiceName' |
| _svc_display_name_='Service Display Name' |
| _svc_description_='Service description.' |
| def__init__(self, *args): |
| super().__init__(*args) |
| defSvcStop(self): |
| self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
| self.process.terminate() |
| # os.kill(os.getpid(), signal.SIGTERM) |
| self.ReportServiceStatus(win32service.SERVICE_STOPPED) |
| defSvcDoRun(self): |
| servicemanager.LogMsg( |
| servicemanager.EVENTLOG_INFORMATION_TYPE, |
| servicemanager.PYS_SERVICE_STARTED, |
| (self._svc_name_, ') |
| ) |
| self.process=Process(target=self.main) |
| self.process.start() |
| self.process.run() |
| defmain(self): |
| pass |
| if__name__'__main__': |
| win32serviceutil.HandleCommandLine(Service) |
commented Feb 2, 2017
In which circumstance are --username and --password parameters used? Are they required only for installing the service as automatic starting? |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment