最近测试hbase,老发现服务挂掉,自己不能及时发现,想了想,写了个脚本,让脚本每个小时执行一次,以便检测是否有服务挂掉,如果有服务挂掉,及时启动
import os
import datetime
class CheckService(object): def __init__(self):
pass
def getService(self):
# service list
service = ['Jps'
,'EmbeddedServer'
,'QuorumPeerMain'
,'NodeManager'
,'ResourceManager'
,'NameNode'
,'SecondaryNameNode'
,'HMaster'
,'HRegionServer'
,'RunJar' ]
# Traversal list
for i in service:
a = "jps | awk '{print $2}'|grep "+i
res = os.system(a)
# Determine whether the service is running
if res != 0 :
print "%s is not running!" %(i)
if i == 'HMaster':
os.system('start-hbase.sh')
elif i == 'HRegionServer':
os.system('local-regionservers.sh start 1')
elif i == 'EmbeddedServer':
os.system('ranger-admin start')
elif i == 'QuorumPeerMain':
os.system('zkServer.sh start')
else:
print 'restart Hadoop !!!' def timerFun(self,sched_Timer):
flag = 0
while True:
now = datetime.datetime.now()
if now == sched_Timer:
self.getService()
flag = 1
else:
if flag == 1:
sched_Timer = sched_Timer + datetime.timedelta(hours=1)
flag = 0 if __name__ == "__main__":
cs = CheckService()
sched_Timer = datetime.datetime(2017,7,25,9,14)
print 'run the timer task at {0}'.format(sched_Timer)
cs.timerFun(sched_Timer)
这个代码也可以扩展,把hours=1改成minutes=1就变成了每个小时定时任务,改成days=1就变成每天的定时任务