Windows service template added
--HG-- branch : aspn
This commit is contained in:
25
src/winsc.py
Normal file
25
src/winsc.py
Normal 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)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
# winservice.py
|
# winservice.py
|
||||||
|
# Recipe 551780 revision 3
|
||||||
|
|
||||||
from os.path import splitext, abspath
|
from os.path import splitext, abspath
|
||||||
from sys import modules
|
from sys import modules
|
||||||
@@ -19,7 +20,7 @@ class Service(win32serviceutil.ServiceFramework):
|
|||||||
import servicemanager
|
import servicemanager
|
||||||
servicemanager.LogInfoMsg(str(msg))
|
servicemanager.LogInfoMsg(str(msg))
|
||||||
def sleep(self, sec):
|
def sleep(self, sec):
|
||||||
win32api.Sleep(sec*1000, True)
|
win32api.Sleep(sec*1000, True)
|
||||||
def SvcDoRun(self):
|
def SvcDoRun(self):
|
||||||
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
|
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
|
||||||
try:
|
try:
|
||||||
@@ -45,13 +46,12 @@ class Service(win32serviceutil.ServiceFramework):
|
|||||||
def stop(self): pass
|
def stop(self): pass
|
||||||
|
|
||||||
def instart(cls, name, display_name=None, stay_alive=True):
|
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
|
cls : the class (derived from Service) that implement the Service
|
||||||
name : Service name
|
name : Service name
|
||||||
display_name : the name displayed in the service manager
|
display_name : the name displayed in the service manager
|
||||||
stay_alive : Service will stop on logout if False
|
stay_alive : Service will stop on logout if False
|
||||||
'''
|
'''
|
||||||
cls._svc_name_ = name
|
cls._svc_name_ = name
|
||||||
cls._svc_display_name_ = display_name or 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
|
# maybe py2exe went by
|
||||||
from sys import executable
|
from sys import executable
|
||||||
module_path=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__)
|
cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)
|
||||||
if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True)
|
if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True)
|
||||||
try:
|
try:
|
||||||
win32serviceutil.InstallService(
|
win32serviceutil.InstallService(
|
||||||
cls._svc_reg_class_,
|
cls._svc_reg_class_,
|
||||||
cls._svc_name_,
|
cls._svc_name_,
|
||||||
cls._svc_display_name_,
|
cls._svc_display_name_,
|
||||||
startType=win32service.SERVICE_AUTO_START
|
startType = win32service.SERVICE_AUTO_START
|
||||||
)
|
)
|
||||||
print 'Install ok'
|
print 'Install ok'
|
||||||
win32serviceutil.StartService(
|
win32serviceutil.StartService(
|
||||||
cls._svc_name_
|
cls._svc_name_
|
||||||
)
|
)
|
||||||
print 'Start ok'
|
print 'Start ok'
|
||||||
except Exception, x:
|
except Exception, x:
|
||||||
print str(x)
|
print str(x)
|
||||||
@@ -90,6 +90,7 @@ def instart(cls, name, display_name=None, stay_alive=True):
|
|||||||
#
|
#
|
||||||
|
|
||||||
# winservice_test.py
|
# winservice_test.py
|
||||||
|
|
||||||
from winservice import Service, instart
|
from winservice import Service, instart
|
||||||
|
|
||||||
class Test(Service):
|
class Test(Service):
|
||||||
@@ -102,4 +103,4 @@ class Test(Service):
|
|||||||
self.runflag=False
|
self.runflag=False
|
||||||
self.log("I'm done")
|
self.log("I'm done")
|
||||||
|
|
||||||
instart(Test, 'aTest', 'Python Service Test')
|
instart(Test, 'aTest', 'Python Service Test')
|
||||||
Reference in New Issue
Block a user