腳本監控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 :將網頁的返回狀態信息寫進文件