Tomcat监测脚本的实现示例
实现效果
通过Tomcat监测脚本按照一定的时间频度来检测Tomcat应用是否正常,如果进程不存在,则启动Tomcat;如果进程存在而访问不正常,则杀掉进程,再启动Tomcat。
脚本的主体
vi/home/dev/ctl/tomcat-inspector/image.sh
#!/bin/bash
echo$(date'+%Y-%m-%d%H:%M:%S')
whoami
JAVA_HOME=/opt/java/jdk1.8.0_40
PATH=$JAVA_HOME/bin:$PATH
exportPATHJAVA_HOME
port=8086
sn="tomcat-image-$port"
th="/home/dev/tomcat/$sn"
url="http://you-app-server/"
tomcat=`ps-ef|grepjava|grep$sn|grep$port|wc-l`
if[$tomcat-eq0]
then
echo$snstoped.
echostarting$sn......
$th/bin/startup.sh
fi
if[$tomcat-ge1]
then
tpid=`ps-ef|grepjava|grep$sn|grep$port|awk'{print$2}'`
echo$snisrunning,pid=$tpid
httpStatus=`curl-I$url2>/dev/null|grepHTTP|awk'{print$2}'`
echo$httpStatus
if[[-z"$httpStatus"||$httpStatus-ge500]]
then
echokilling$sn......
kill-9$tpid
echostarting$sn......
$th/bin/startup.sh
fi
fi
设定执行频度
crontab-e */5****/home/dev/ctl/tomcat-inspector/image.sh>>/home/dev/ctl/tomcat-inspector/logs/image.log2>&1
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对毛票票的支持。