Created
August 6, 2014 01:40
-
-
Save steelywing/8217e66711552a126980 to your computer and use it in GitHub Desktop.
python windows service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?