监控tomcat程序是否正常运行的脚本#!/usr/local/bin/python3.5#coding:utf-8import urllib.requestimport subprocess, osfrom time import strftime, gmtime, sleep #tomcat_pid = (((subprocess.Popen("lsof -i :8080 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()).strip()req = urllib.request.Request('http://123.57.79.138')try: response = urllib.request.urlopen(req, timeout = 30)except Exception as e: with open('/var/tomcat/logs/tomcat_down.txt', 'a') as f: current_time = '#================<< Tomcat Down Time : ' + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' >>==============\nMore information about tomcat...\n' f.write(current_time) f.write(e) tomcat_status = (open('/proc/' + tomcat_pid + '/status')).read() f.write(tomcat_status + '\n') subprocess.Popen("/var/tomcat/bin/shutdown.sh > /dev/null 2>&1", shell=True, stdout=subprocess.PIPE) if len(tomcat_pid) == 0: pass else: subprocess.Popen("kill -9 " + tomcat_pid, shell=True, stdout=subprocess.PIPE) subprocess.Popen("rm -rf /var/tomcat/temp/*", shell=True, stdout=subprocess.PIPE) subprocess.Popen("rm -rf /var/tomcat/work/Catalina/localhost/*", shell=True, stdout=subprocess.PIPE) sleep(3) if len(os.listdir('/var/tomcat/temp')) == 0 and len(os.listdir('/var/tomcat/work/Catalina/localhost')) == 0: subprocess.Popen("/var/tomcat/bin/startup.sh", shell=True, stdout=subprocess.PIPE) send_mail.SendMail('/var/tomcat/logs/tomcat_lastdown.txt') else: with open('/var/tomcat/logs/tomcat_down.txt', 'a') as f: f.write('The cache file not clean yet!')END!