Skip to content

Instantly share code, notes, and snippets.

@steelywing
Created August 6, 2014 01:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steelywing/8217e66711552a126980 to your computer and use it in GitHub Desktop.
Save steelywing/8217e66711552a126980 to your computer and use it in GitHub Desktop.
python windows service
import win32serviceutil
import win32service
import win32event
import servicemanager
# import signal
# import os
from multiprocessing import Process
from main import app
# 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
class Service(win32serviceutil.ServiceFramework):
_svc_name_ = "ServiceName"
_svc_display_name_ = "Service Display Name"
_svc_description_ = "Service description."
def __init__(self, *args):
super().__init__(*args)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.process.terminate()
# os.kill(os.getpid(), signal.SIGTERM)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
def SvcDoRun(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()
def main(self):
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(Service)
@Tinoky
Copy link

Tinoky commented Feb 2, 2017

In which circumstance are --username and --password parameters used? Are they required only for installing the service as automatic starting?
Could you please give an example of the username including the domain name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment