From eb95bcb83e60cec4f784a3de5f9cca17be4bbadf Mon Sep 17 00:00:00 2001 From: baloan Date: Fri, 20 Apr 2012 20:51:46 +0200 Subject: [PATCH] Windows service template added --HG-- branch : aspn --- src/winsc.py | 25 +++++++++++++++++++++++++ src/winservice.py | 33 +++++++++++++++++---------------- 2 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 src/winsc.py diff --git a/src/winsc.py b/src/winsc.py new file mode 100644 index 0000000..a07570e --- /dev/null +++ b/src/winsc.py @@ -0,0 +1,25 @@ +# http://code.activestate.com/recipes/59872-manipulating-windows-services/ + +import win32serviceutil + +def service_info(action, service): + if action == 'stop': + win32serviceutil.StopService(service) + print '%s stopped successfully' % service + elif action == 'start': + win32serviceutil.StartService(service) + print '%s started successfully' % service + elif action == 'restart': + win32serviceutil.RestartService(service) + print '%s restarted successfully' % service + elif action == 'status': + if win32serviceutil.QueryServiceStatus(service)[1] == 4: + print "%s is running normally" % service + else: + print "%s is *not* running" % service + +if __name__ == '__main__': + machine = 'cr582427-a' + service = 'Zope23' + action = 'start' + service_info(action, service) \ No newline at end of file diff --git a/src/winservice.py b/src/winservice.py index 0367981..1130d05 100644 --- a/src/winservice.py +++ b/src/winservice.py @@ -1,4 +1,5 @@ # winservice.py +# Recipe 551780 revision 3 from os.path import splitext, abspath from sys import modules @@ -19,7 +20,7 @@ class Service(win32serviceutil.ServiceFramework): import servicemanager servicemanager.LogInfoMsg(str(msg)) def sleep(self, sec): - win32api.Sleep(sec*1000, True) + win32api.Sleep(sec*1000, True) def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_START_PENDING) try: @@ -45,13 +46,12 @@ class Service(win32serviceutil.ServiceFramework): def stop(self): pass def instart(cls, name, display_name=None, stay_alive=True): - ''' - Install and Start (auto) a Service + ''' Install and Start (auto) a Service - cls : the class (derived from Service) that implement the Service - name : Service name - display_name : the name displayed in the service manager - stay_alive : Service will stop on logout if False + cls : the class (derived from Service) that implement the Service + name : Service name + display_name : the name displayed in the service manager + stay_alive : Service will stop on logout if False ''' cls._svc_name_ = name cls._svc_display_name_ = display_name or name @@ -61,20 +61,20 @@ def instart(cls, name, display_name=None, stay_alive=True): # maybe py2exe went by from sys import executable module_path=executable - module_file=splitext(abspath(module_path))[0] + module_file = splitext(abspath(module_path))[0] cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__) if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True) try: win32serviceutil.InstallService( - cls._svc_reg_class_, - cls._svc_name_, - cls._svc_display_name_, - startType=win32service.SERVICE_AUTO_START - ) + cls._svc_reg_class_, + cls._svc_name_, + cls._svc_display_name_, + startType = win32service.SERVICE_AUTO_START + ) print 'Install ok' win32serviceutil.StartService( - cls._svc_name_ - ) + cls._svc_name_ + ) print 'Start ok' except Exception, x: print str(x) @@ -90,6 +90,7 @@ def instart(cls, name, display_name=None, stay_alive=True): # # winservice_test.py + from winservice import Service, instart class Test(Service): @@ -102,4 +103,4 @@ class Test(Service): self.runflag=False self.log("I'm done") -instart(Test, 'aTest', 'Python Service Test') +instart(Test, 'aTest', 'Python Service Test') \ No newline at end of file