Tomcat 自动监控脚本

脚本监控Tomcat服务,当服务死掉后脚本自动重启服务。

1、将本脚本保存为/root/tcautorestart.sh,并给执行权限。

#!/bin/bash
if [[ -e /root/tcstatus.log ]];then
cp -f /dev/null /root/tcstatus.log
fi
/usr/local/bin/lynx -dump -connect_timeout=15000 -error_file=/root/tcstatus.log        /
http://downtheme.somode.com:8080/index.jsp         >/dev/null         ##index.jsp
为用于测试的jsp
cat /root/tcstatus.log | grep "STATUS=HTTP/1.1 200 OK" >/dev/null

if (( $? != 0 ));then
{
#shutdown tomcat
date >> /root/tcrestart.log
while true
do
/usr/local/tomcat/bin/shutdown.sh         >> /root/tcrestart.log
sleep 20
cp -f /dev/null /root/process.tc
ps -aux | awk '$0 ~ /java.*tomcat/{print $0}' > /root/process.tc
cat process.tc | grep "java.*tomcat" > /dev/null
if (( $? == 0 ));then
           continue
else
{
           echo "Tomcat shutdown success!" >> /root/tcrestart.log
           break
}
fi
done

#startup tomcat
while true
do
/usr/local/tomcat/bin/startup.sh         >> /root/tcrestart.log
sleep 5
cp -f /dev/null /root/process.tc
ps -aux | awk '$0 ~ ///bin//java/{print $0}' > /root/process.tc
cat process.tc |grep "/bin/java" > /dev/null
if (( $? == 0 ));then
{
           echo "Tomcat restartup success!" >> /root/tcrestart.log
           break
}
else
continue
fi
done
}
else
exit 0
fi

2
、安装lynx
# tar -xzvf lynx2.8.5.tar.gz
# cd ynx2.8.5
# make
# make install         ##lynx
默认安装到/usr/local/lynx
# ln -s /usr/local/lynx /bin           ##
注意自动化任务的path变量和bash的可能不同,
                        ##
一个能在bash中运行的很好的脚本不一定在cron中运行的很好,有些命令可能在cron中找不到
3
、将tomcat自启动脚本加到cron
# crontab -e
#tomcat auto restart
自动重启
*/5 * * * * /root/tcautorestart.sh > /dev/null 2>&1

参考:

得到网页的返回状态:

[root@toolsbook root]# lynx -dump -connect_timeout=15000 -error_file=/root/test1.log /
http://downtheme.somode.com:8080/index.jsp

          0                       ##
显示浏览页的内容

[root@toolsbook root]# less /root/test1.log               
##网页的返回状态信息

          URL=http://downtheme.somode.com:8080/index.jspdf (GET)
STATUS=HTTP/1.1 404 /index.jspdf          
##网页不存在,返回404错误

          URL=http://downtheme.somode.com:8080/index.jsp (GET)
STATUS=HTTP/1.1 200 OK                 
##网页能正常访问

lynx参数:
           -dump
:显示网页内容到标准输出并推出
           -connect_timeout
:设置超时时间,单位(毫秒)
           -error_file
:将网页的返回状态信息写进文件

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章