-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.py
63 lines (51 loc) · 1.97 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import win32serviceutil
import win32service
import win32event
import os
import logging
import inspect
import sys
import servicemanager
class IFaceAttReaderMonitor(win32serviceutil.ServiceFramework):
_svc_name_ = "IFaceAttReaderMonitor"
_svc_display_name_ = "IFaceAttReader Monitor service"
_svc_description_ = "This is a python service monitor code "
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.run = True
def _getLogger(self):
logger = logging.getLogger('[IFaceAttReaderMonitor]')
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
handler = logging.FileHandler(os.path.join(dirpath, "monitor-service.log"))
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):
import wmi
import time
c = wmi.WMI()
while True:
for service in c.Win32_Service(Name="IFaceAttReader"):
result, = service.StartService()
if result == 0:
self.logger.info("Service " + service.Name + " started")
elif result == 10:
self.logger.info("service is running.")
else:
self.logger.info("Some problem" + str(result))
break
else:
self.logger.info("Service not found")
time.sleep(300)
def SvcStop(self):
self.logger.info("monitor service is stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.run = False
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(IFaceAttReaderMonitor)