检测逻辑:连续3次访问tomcat提供的接口,若3次响应码都不为200,则自动重启tomcat 脚本内容:
#!/bin/bash source /etc/profile IP=10.78.1.183 PORT=8114 TOMCAT_DIR=/soft/tomcat_8114 #定义发送邮件函数 function send_mail(){ maillist=( linuxidc@linuxidc.com ) DATE=`date '%F %T'` SUBJECT="${IP}的${PORT}端口发生故障,已自动重启!" CONTENT="时间:${DATE}n主机:${IP}n事件:${PORT}端口发生重启" for mail in ${maillist[*]};do echo -e "${CONTENT}" | mail -s "${SUBJECT}" $mail done } #对接口访问3次 declare -i NUM=0 for i in `seq 1 3`;do CODE=$(/usr/bin/curl -I -m 10 -o /dev/null -s -w %{http_code}"n" http://${IP}:${PORT}) if [ $CODE == "000" ];then ((NUM )) fi sleep 3 done #根据NUM的值来确定是否重启tomcat,NUM=3时进行重启 if [ $NUM == 3 ];then IS_LIVING=`jps -m -l -v | grep "${PORT}" | wc -l` if [ ${IS_LIVING} -eq 1 ];then PID=`jps -m -l -v | grep "${PORT}" | awk '{print $1}'` kill -9 $PID ${TOMCAT_DIR}/bin/startup.sh elif [ ${IS_LIVING} -eq 0 ];then ${TOMCAT_DIR}/bin/startup.sh fi send_mail fi
任务计划:每5分钟检测一次
*/5 * * * * /server/scripts/tomcat/check_8114.sh &> /dev/null