Linux shell自动监控tomcat宕机重启

参考网上例子 整了个 linux 的自动检测tomcat宕机的shell脚本

以后就可以自己重启了  出了问题。。(有时间放到linux定时任务计划里面)\


下面检测的是一个工程里面的图片 来检测tomcat 是否正常

echo 001 这种打断点使用的 可以自己去掉


#!/bin/bash
#author: flyer
NOL=$(ps -ef |grep tomcat|awk '{print $2}')
# echo $NOL
SYS=`uname -a|awk '{print $2}'`
TLOG=/var/log/tomcat.log
PID=/tmp/tomcat
TOMCAT_HOME=/alidata/server/apache-tomcat-7.0.62
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh  
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh
DATE=`date +%Y/%m/%d_%H:%M:%S`
RESULT=`/usr/bin/wget -S --spider --tries=3 --timeout=3 http://localhost:8080/bimatrixweb/resources/images/logo_bimatrix.png 2>&1 | grep HTTP/1.1|awk '{print $2}'` 	 
echo $RESULT
##############PID isn't in#################
echo $PID
##############tomcat####################### 


TOMCATPID=`ps -ef|grep tomcat|grep java|grep -v "grep"|awk '{print $2}'`
echo $TOMCATPID
if [ "T$RESULT" == "T200" ]; then	
	echo 003
    echo -ne "$SYS $DATE Tomcat is OK!">>$TLOG
	#echo "$SYS is $NOL about tomcat restart ok"|mail -s $SYS [email protected]
 else
	echo 004
    echo -ne "$DATE Tomcat is down !!">>$TLOG
    #echo "$SYS is $NOL about tomcat"|mail -s $SYS [email protected]	
	#TOMCATPID=`ps -ef|grep tomcat|grep java|grep -v "grep"|awk '{print $2}'`
    if [ ! -z "$TOMCATPID" ];then	
		#echo $TOMCATPID		
		
		#echo  008
		kill -9 $TOMCATPID  #放开
		#echo $TOMCATPID
        sleep 4
    fi
	   echo 005    
	   echo 开始启动	   
	   
	   $TOMCAT_START
	   #sleep 10
	   echo "$DATE $SYS'tomcat is restarting"  >>$TLOG
       if [ "T$RESULT" == "T200" ]; then
	      echo 006
          echo -ne "$SYS $DATE is $NOL about tomcat restart ok!!" >>$TLOG
          #echo "$SYS is $NOL about tomcat restart ok"|mail -s $SYS [email protected]
       
       else
		  echo 007
          echo -ne "SYS $DATE is $NOL about tomcat failure" >>$TLOG
         # echo "TOMCAT restart failure $SYS is $NOL "|mail -s $SYS $NOL [email protected]  
       fi	
 fi
#fi








参考:http://blog.chinaunix.net/uid-25046147-id-3078397.html


8.1修改了一版本 增加了文件锁 

防止上次脚本还没有执行 下次脚本开始了 开始时候创建个文件 执行完之后删除


然后加入jdk的 环境变量 脚本加入linux的定时计划 不然会报错  

在提示root     13745 31098  0 19:46 pts/4    00:00:00 grep --color=auto tomcat
您在 /var/mail/root 中有新邮件 

 /var/mail/root  root 文件发现错误

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined.

加入环境变量

export JAVA_HOME=/opt/java/jdk1.7.0_75
export JRE_HOME=/opt/java/jdk1.7.0_75/jre


完整代码加入 

#!/bin/bash
NOL=$(ps -ef |grep tomcat|awk '{print $2}')
# echo $NOL
SYS=`uname -a|awk '{print $2}'`
TLOG=/var/log/tomcat.log
PID=/tmp/tomcat
TOMCAT_HOME=/home/thomas/apache-tomcat-7.0.59 
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh  
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh

export JAVA_HOME=/opt/java/jdk1.7.0_75
export JRE_HOME=/opt/java/jdk1.7.0_75/jre
DATE=`date +%Y/%m/%d_%H:%M:%S`
#RESULT=`/usr/bin/wget -S --spider --tries=3 --timeout=3 http://localhost:8080/revitbus/resources/assets/base/img/layout/logos/logo-1.png 2>&1 | grep HTTP/1.1 | awk '{print $2}'  `
#方式1 图片 
#RESULT=`/usr/bin/wget -S --spider --tries=3 --timeout=3 http://192.168.2.115:8080/revitbus/resources/assets/base/img/layout/logos/logo-1.png 2>&1 | grep HTTP/1.1|awk '{print $2}'` 	 
#方式2 请求页面
RESULT=`/usr/bin/wget -S --spider --tries=3 --timeout=3 http://192.168.2.115:8080/revitbus/news/contactus.html 2>&1 | grep HTTP/1.1|awk '{print $2}'` 	 
#这里定时获取的连接   可以是一个图片 也可以是一个不怎么经常被访问的页面最好比较小的 消耗比较小


echo $RESULT
##############PID isn't in#################
echo $PID
##############tomcat####################### 

TOMCATPID=`ps -ef|grep tomcat|grep java|grep -v "grep"|awk '{print $2}'`
echo $TOMCATPID

#echo "有其他脚本正在执行,本次退出执行" >$PID 相当于文件锁 防止两个程序同时启动一个宕机的程序
if [ -f $PID ];then
	echo "文件存在 文件在$filepath" 
	echo "本次程序退出"
		 exit;
	else
	echo "文件不存在 没有文件锁 本次脚本继续往下执行.."
	echo  "本程序开始执行本次定时任务" >$PID     #加入文件锁
fi
#mutt -s "Test mail" [email protected] < /var/log/tomcat.log #$TLOG

if [ "T$RESULT" == "T200" ]; then	
	
	echo 003
    #echo -ne "$SYS $DATE Tomcat is OK!">>$TLOG
	#echo "$SYS is $NOL about tomcat restart ok"|mail -s $SYS [email protected]
 else
	echo 004
    echo -ne "$DATE Tomcat is down !!">>$TLOG
	
    #echo "$SYS is $NOL about tomcat"|mail -s $SYS [email protected]	
	#TOMCATPID=`ps -ef|grep tomcat|grep java|grep -v "grep"|awk '{print $2}'`
    if [ ! -z "$TOMCATPID" ];then	
		#echo $TOMCATPID
		echo  008
		kill -9 $TOMCATPID  #放开
		#echo $TOMCATPID
        sleep 4
    fi
	   echo 005    
	   # echo 1 >$PID    
	   echo 开始启动
	   $TOMCAT_START
	   #sleep 10
	   echo "$DATE $SYS'tomcat is restarting"  >>$TLOG
       if [ "T$RESULT" == "T200" ]; then
	      echo 006
          echo -ne "$SYS $DATE is $NOL about tomcat restart ok!!" >>$TLOG
          #echo "$SYS is $NOL about tomcat restart ok"|mail -s $SYS [email protected]
          # rm -rf $PID
       else
		  echo 007
          echo -ne "SYS $DATE is $NOL about tomcat failure" >>$TLOG
         # echo "TOMCAT restart failure $SYS is $NOL "|mail -s $SYS $NOL [email protected]  
       fi

 fi
rm -rf $PID #程序处理完删除文件锁
#fi





定时计划 

# For more information see the manual pages of crontab(5) and cron(8)
*/20  * * * *    /home/bowen/tomcatMonitorNew.sh 
# */1 * * * * echo “Hello World.” >> /alidata/test

关于定时任务 参考文章

http://blog.csdn.net/albertfly/article/details/51338295



注意使用的时候 记得给这个sh 可执行文件赋权限

chmod 777  tomcatMonitorNew.sh 

你可能感兴趣的:(linux,shell)