本文共 1166 字,大约阅读时间需要 3 分钟。
检测逻辑:连续3次访问tomcat提供的接口,若3次响应码都不为200,则自动重启tomcat
脚本内容:#!/bin/bashsource /etc/profileIP=10.78.1.183PORT=8114TOMCAT_DIR=/soft/tomcat_8114#定义发送邮件函数function send_mail(){ maillist=( zhengwei.liu@xxx.cn ) 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=0for 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 3done#根据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_mailfi
任务计划:每5分钟检测一次
*/5 * * * * /server/scripts/tomcat/check_8114.sh &> /dev/null
转载于:https://blog.51cto.com/liuzhengwei521/2135548