有时候会碰到在Tomcat下的网站不能访问的时候,该脚本会自动重启工程。虽然不一定能彻底解决问题,但是能解决一些由于Tomcat本身的问题而造成的无法访问,所以仅作为临时使用罢了。脚本会根据设定的任务不停的检测站点的访问情况,当不能访问时,自动开始修复。

新建 tomcat-check.sh文件,把下面的代码扔进去:

#!/bin/sh
######################
#Version: v1.0
#Date: 2015/10/13
######################

# Tomact Path
ServerName=YUNStation #服务名称
TomcatPath=/root/http/website #替换成你的服务目录
WebUrl=http://www.baidu.com #替换成你的地址
WePID=$(ps -ef |grep '\/station\/conf' |grep -v 'grep'|awk '{print $2}')

#Do not edit the following content!!!
TomcatCache=$TomcatPath/work
StartTomcat=$TomcatPath/bin/startup.sh
WeNullPath=/dev/null
WeLogPath=/var/log/welog-$ServerName.log


WeMonitor()
{
  echo "=========== Start Monitoring [$(date +'%F %H:%M:%S')] ==========="
  if [ $WePID ];then
    echo "[INFO]TomcatPID is:$WePID,Continue testing..."
    WeStatusCode=$(curl -s -o $WeNullPath -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
    if [ $WeStatusCode -eq 200 ];then
        echo "[INFO]Page Status Code is : $WeStatusCode,Tomcat run successfully,WebSite is ok!"
    else
        echo "[ERROR]Page access error, error code is: $WeStatusCode,For more information please check : $WeLogPath"
        echo "[ERROR]Page access error, restart tomcat...."
        kill -9 $WePID  # kill Tomcat PID
        sleep 3
        rm -rf $TomcatCache # Cleanup TomcatCache
        $StartTomcat
        echo "[INFO]successfully!"
        echo "CMCC-$ServerName Restart Time: $(date +'%F %H:%M:%S')" | mail -s "$ServerName-Restart - $(date +'%H:%M:%S')" xxx@qq.com
    fi
  else
    echo "[ERROR]Tomcat is not running, restarting..."
    echo "[INFO]Please waiting..."
    rm -rf $TomcatCache
    $StartTomcat
    echo "[INFO]successfully!"
     echo "CMCC-$ServerName Restart Time: $(date +'%F %H:%M:%S')" | mail -s "$ServerName-Restart - $(date +'%H:%M:%S')" xxx.qq.com
  fi
  echo ""
}
WeMonitor>>$WeLogPath

给这个脚本加个定期运行,就行。


文章链接: http://www.gongzi.org/tomcat-check-url-restart.html  [复制]
站方声明: 除特别标注, 本站所有文章均为原创, 互联分享, 尊重版权, 转载请注明.