Created
April 20, 2018 09:23
-
-
Save kskmori/6b58f3d9a62f6d32246af22c112264c2 to your computer and use it in GitHub Desktop.
python windows service test script
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
#!/usr/bin/env python | |
import logging | |
import os | |
import signal | |
import sys | |
import win32serviceutil | |
logging.basicConfig( | |
filename = 'C:\\debug-log.txt', | |
level = logging.DEBUG, | |
format = '%(asctime)s %(levelname)s %(message)s' | |
) | |
class SampleService(win32serviceutil.ServiceFramework): | |
_svc_name_ = 'KSKSampleService' | |
_svc_display_name_ = 'KSK Sample Service' | |
_svc_description_ = 'KSK Sample Service' | |
pid = 0 | |
def __init__(self, args): | |
logging.debug('__init__(args) = %s' % str(args)) | |
logging.debug('sys.argv = %s' % sys.argv) | |
logging.debug('_svc_name_ = %s, _exe_args_ = %s' % (self._svc_name_, self._exe_args_)) | |
svc_name = args[0] | |
logging.debug('svc_name = %s' % str(svc_name)) | |
self.hostname = win32serviceutil.GetServiceCustomOption(svc_name, 'hostname', '') | |
logging.debug('self.hostname = %s' % self.hostname) | |
SampleService.set_svc_name(sys.argv[1]) | |
win32serviceutil.ServiceFramework.__init__(self, args) | |
#super(SampleService, self).__init__(args) | |
@classmethod | |
def set_svc_name(cls, hostname): | |
cls._svc_name_ = 'KSKSampleService (%s)' % hostname | |
cls._svc_display_name_ = 'KSK Sample Service (%s)' % hostname | |
cls._svc_description_ = 'kSK Sample Service for %s' % hostname | |
cls._exe_args_ = hostname | |
def SvcDoRun(self): | |
self.run = True | |
self.pid = os.getpid() | |
logging.debug('%d,%s: %s' % (self.pid, self._svc_name_, sys.argv)) | |
while self.run: | |
import time, datetime | |
time.sleep(1) | |
logging.debug('%d,%s: running' % (self.pid, self._svc_name_)) | |
def SvcStop(self): | |
self.run = False | |
#os.kill(self.pid, signal.SIGTERM) | |
@classmethod | |
def install(cls, hostname): | |
argv = [sys.argv[0], 'install'] | |
win32serviceutil.HandleCommandLine(SampleService, argv=argv) | |
win32serviceutil.SetServiceCustomOption(cls, 'hostname', hostname) | |
@classmethod | |
def start(cls): | |
argv = [sys.argv[0], 'start'] | |
win32serviceutil.HandleCommandLine(SampleService, argv=argv) | |
@classmethod | |
def stop(cls): | |
argv = [sys.argv[0], 'stop'] | |
win32serviceutil.HandleCommandLine(SampleService, argv=argv) | |
@classmethod | |
def remove(cls): | |
argv = [sys.argv[0], 'remove'] | |
win32serviceutil.HandleCommandLine(SampleService, argv=argv) | |
def main_cmd_args(): | |
print sys.argv | |
cmd = sys.argv[1] | |
hostname = sys.argv[2] | |
#sys.argv = sys.argv[0:1] + sys.argv[2:] | |
#print hostname | |
#print sys.argv | |
logging.debug('argv = %s' % sys.argv) | |
ccc = SampleService | |
#if hostname == 'node02': | |
# ccc = SampleService2 | |
# print "use SampleService2" | |
#else: | |
# ccc = SampleService | |
# print "use SampleService" | |
ccc.set_svc_name(hostname) | |
if cmd == 'install': | |
ccc.install(hostname) | |
elif cmd == 'start': | |
ccc.start() | |
elif cmd == 'stop': | |
ccc.stop() | |
elif cmd == 'remove': | |
ccc.remove() | |
elif cmd == 'debug': | |
win32serviceutil.HandleCommandLine(ccc, argv=[sys.argv[0], 'debug']) | |
elif cmd == 'help': | |
win32serviceutil.HandleCommandLine(ccc, argv=[]) | |
else: | |
print "ERROR cmd: %s" % cmd | |
def main_simple(): | |
win32serviceutil.HandleCommandLine(SampleService) | |
if __name__ == '__main__': | |
#main_simple() | |
main_cmd_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment