CLOUDSTACK-5177: Fixed issue with running script from cron job

This commit is contained in:
Jayapal 2013-11-15 12:04:38 +05:30
parent 9410423555
commit 7dceca5995
2 changed files with 19 additions and 22 deletions

View File

@ -64,7 +64,7 @@ crontab -l | grep -v monitorServices.py | crontab -
create_config $config create_config $config
#add cron job #add cron job
(crontab -l ; echo "*/3 * * * * python /root/monitorServices.py") | crontab - (crontab -l ;echo -e "SHELL=/bin/bash\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n */1 * * * * /usr/bin/python /root/monitorServices.py") | crontab -
unlock_exit 0 $lock $locked unlock_exit 0 $lock $locked

View File

@ -62,8 +62,13 @@ def getConfig( config_file_path = "/etc/monitor.conf" ):
return process_dict return process_dict
def printd (msg): def printd (msg):
return 0 return 0
print msg
f= open(monitor_log,'r+')
f.seek(0, 2)
f.write(str(msg)+"\n")
f.close()
def raisealert(severity, msg, process_name=None): def raisealert(severity, msg, process_name=None):
#timeStr=str(time.ctime()) #timeStr=str(time.ctime())
@ -76,12 +81,6 @@ def raisealert(severity, msg, process_name=None):
pout = Popen(msg, shell=True, stdout=PIPE) pout = Popen(msg, shell=True, stdout=PIPE)
#f= open(monitor_log,'r+')
#f.seek(0, 2)
#f.write(str(log))
#f.close()
def isPidMatchPidFile(pidfile, pids): def isPidMatchPidFile(pidfile, pids):
if pids is None or isinstance(pids,list) != True or len(pids) == 0: if pids is None or isinstance(pids,list) != True or len(pids) == 0:
@ -119,7 +118,7 @@ def checkProcessStatus( process ):
service_name = process.get('servicename') service_name = process.get('servicename')
pidfile = process.get('pidfile') pidfile = process.get('pidfile')
#temp_out = None #temp_out = None
restartFailed=0 restartFailed=False
pidFileMatched=1 pidFileMatched=1
cmd='' cmd=''
if process_name is None: if process_name is None:
@ -186,34 +185,32 @@ def checkProcessStatus( process ):
for pid in pids: for pid in pids:
cmd = 'kill -9 '+pid; cmd = 'kill -9 '+pid;
printd(cmd) printd(cmd)
Popen(cmd, shell=True, stdout=PIPE) Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
cmd = 'service ' + service_name + ' restart' cmd = 'service ' + service_name + ' restart'
try:
time.sleep(1) time.sleep(1)
return_val= check_call(cmd , shell=True) #return_val= check_call(cmd , shell=True)
except CalledProcessError:
restartFailed=1 cout = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
msg="service "+ process_name +" restart failed" return_val = cout.wait()
printd(msg)
continue
if return_val == 0: if return_val == 0:
printd("The process" + process_name +" recovered successfully ") printd("The process" + process_name +" recovered successfully ")
msg="The process " +process_name+" is recovered successfully " msg="The process " +process_name+" is recovered successfully "
raisealert(log.INFO,process_name,msg) raisealert(log.INFO,msg,process_name)
break; break;
else: else:
#retry restarting the process for few tries #retry restarting the process for few tries
printd("process restart failing trying again ....") printd("process restart failing trying again ....")
restartFailed=1 restartFailed=True
time.sleep(1) time.sleep(1)
continue continue
#for end here #for end here
if restartFailed == 1: if restartFailed == True:
msg="The process %s recover failed ", process_name; msg="The process %s recover failed "%process_name
raisealert(log.ALERT,process_name,msg) raisealert(log.ALERT,process_name,msg)
printd("Restart failed after number of retries") printd("Restart failed after number of retries")