参考:http://www.linuxidc.com/Linux/2012-11/74003.htm #!/bin/sh
monitorlog=/opt/tomcat7/logs/monitor.log monitor()
{
tomcatid=$(ps -ef |grep tomcat |grep -w 'tomcat7'|grep -v 'grep'|awk '{print $2}')
tomcatup=/opt/tomcat7/bin/startup.sh
tomcatcache=/opt/tomcat7/work
tomcaturl=http://www.baidu.com/ok.jsp echo "[info]$(date +'%F %H:%M:%S')"
echo "[info]($tomcatid)"
if [ $tomcatid ]; then
http_code=$(curl -s -o /dev/null --connect-timeout 10 -m 20 $tomcaturl -w %{http_code})
if [ $http_code -eq 200 ]; then
echo "[info]tomcat ok"
else
echo "[error]tomcat died ($http_code)"
kill -9 $tomcatid
sleep 3
rm -rf $tomcatcache
$tomcatup
fi
else
echo "[error]tomcat none"
echo "[info]tomcat start"
rm -rf $tomcatcache
$tomcatup
fi
} monitor>>$monitorlog