Add this to this file.
crontab -e
*/5 * * * * /usr/local/bin/mcJobMonitor.py >> /dev/null
If you want to know the meaning of this sentence, please go to Cron on Linux.
This mcJobMonitor.py is
#!/usr/bin/python
#//////////////////////////////////////////////////
# Haifeng Li
#///////////////////////////////////////////////////
import sys, os, time, commands, getopt
def SendMailtoUser(ScriptName ):
hostname = commands.getoutput("uname -n")
timedate = commands.getoutput("date")
pwd = commands.getoutput("pwd")
message = "Your script "+ScriptName+" in machine " +hostname+ " is dead. \n"\
+"We have restarted this script. You can go to this machine to check the reason. \n"
email = "xxxxxxx@gmail.com"
command ="".join(["echo \"", message, \
"\" | mail ", email, " -s \"Cron Monitor for ", ScriptName, "\""])
os.system(command)
if __name__=="__main__" :
#________________________updatedatabase.py________________________________
ScriptName = "/var/www/html/hpeng3/script/updatedatabase.py"
status = commands.getoutput("ps -ef | grep 'updatedatabase.py' | grep -v grep")
status = status.strip()
if status == '' :
pushd = "pushd /var/www/html/hpeng3/script"
restart = "./restart.sh"
popd = "popd"
command = ";".join([pushd, restart, popd])
os.system(command)
SendMailtoUser(ScriptName )