Windows service template added

--HG--
branch : aspn
This commit is contained in:
baloan
2012-04-20 20:51:46 +02:00
parent 88b8c9d693
commit eb95bcb83e
2 changed files with 42 additions and 16 deletions

25
src/winsc.py Normal file
View File

@@ -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)

View File

@@ -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):