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
:将网页的返回状态信息写进文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值