最近写了个简单的tomcat检测脚本。可以进行简单的进程和页面检测。
#!/bin/bash # author:wangxingwei # date:2015/9/1 # fun:检测tomcat进程和页面是否正常 StartTomcat=/home/aspire/apps/tomcat/bin/startup.sh TomcatCache=/home/aspire/apps/tomcat/work TomcatCheckLog=/home/aspire/logs/check_tomcat_log TomcatName=tomcat TomcatPort=8080 # 定义要监控的页面地址 WebUrl=http://localhost:$TomcatPort StartTom() #启动tomcat函数 { echo "[info] 开始启动tomcat" $StartTomcat TomcatNum=`ps aux|grep $TomcatName|grep -Ev "grep|$0"|awk -F " " ‘{print $2}‘|wc -l` if [ $TomcatNum -eq 0 ];then echo "[error]Tomcat启动失败" else echo "[info]Tomcat启动成功" fi } KillTom() #杀死tomcat函数 { echo "[info]开始杀死Tomcat进程" ps aux|grep $TomcatName|grep -Ev "grep|$0"|awk -F " " ‘{print $2}‘|xargs kill -9 #杀掉tomcat进程 TomcatNum=`ps aux|grep $TomcatName|grep -Ev "grep|$0"|awk -F " " ‘{print $2}‘|wc -l` #检测是否杀掉 if [ $TomcatNum -eq 0 ];then echo "[info]Tomcat 进程全部杀死" else echo "[error]Tomcat 进程没有被杀死" fi } CheckPage() { CheckPageNum=3 while [ $CheckPageNum -ne 0 ] do if [ $CheckPageNum -eq 1 ];then echo "[info]开始进行最后一次页面检测" else echo "[info]正在进行第$((4-$CheckPageNum))次页面检测............" fi sleep 10 TomcatServiceCode=$(curl -I -m 10 $WebUrl -o /dev/null -s -w %{http_code}) if [ $TomcatServiceCode -eq 200 ];then echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常......" break else echo "[error]tomcat页面出错,请注意......状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo" echo "[error]页面访问出错,开始重启tomcat" KillTom # 杀掉原tomcat进 sleep 3 rm -rf $TomcatCache # 清理tomcat缓存 StartTom CheckPageNum=$(($CheckPageNum-1)) fi done } CheckTomcat() { echo "----start--------`date`--------------------------------" TomcatID=`ps aux| grep $TomcatName|grep -Ev "$0|grep"|awk -F " " ‘{print $2}‘` if [ -n "$TomcatID" ];then # 这里判断TOMCAT进程是否存在 echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..." CheckPage else echo "[error]tomcat进程不存在!tomcat开始自动重启..." echo "[info]$StartTomcat,请稍候......" rm -rf $TomcatCache $StartTomcat TomcatID=`ps aux| grep $TomcatName|grep -Ev "$0|grep"|awk -F " " ‘{print $2}‘` echo "[info]Tomcat进程为$TomcatID,开始检测页面" CheckPage fi echo "----end----------`date`--------------------------------" } CheckTomcat >> $TomcatCheckLog
本文出自 “鱼香肉丝” 博客,请务必保留此出处http://19111010.blog.51cto.com/9410349/1845128
原文地址:http://19111010.blog.51cto.com/9410349/1845128