工作所需,匆匆忙忙写了个监控tomcat的shell脚本,大概思路是这样的:先检测tomcat进程是否存在,如果不存在就启动,如果进程存在,检测页面返回码状态,如果是200就是正常,如果不是就重启。
#!/bin/sh
# func:自动监控tomcat脚本并且执行重启操作
# mail:[email protected]
# date:13/04/2015
# 定义环境变量
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.5.0_11/bin
export JAVA_HOME=/usr/java/jdk1.5.0_11
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
# DEFINE
# 获取tomcat进程ID
TomcatID=$(ps -ef |grep tomcat |grep -'tomcat1'|grep -v 'grep'|awk '{print $2}')
# tomcat启动程序(这里注意tomcat实际安装的路径)
StartTomcat=/opt/aiwm/tomcat/tomcat_1/bin/startup.sh
TomcatCache=/opt/aiwm/tomcat/tomcat_1/work
# 定义要监控的页面地址
WebUrl=http://localhost:8080/DocumentPreviewDemo/
# 日志输出
GetPageInfo=/opt/aiwm/tomcat/tomcat_1/logs/TomcatMonitor.Info
TomcatMonitorLog=/opt/aiwm/tomcat/tomcat_1/logs/TomcatMonitor.log
Monitor()
{
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
if [ $TomcatID ];then # 这里判断TOMCAT进程是否存在
echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..."
# 检测是否启动成功(成功的话页面会返回状态"200")
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常......"
else
echo "[error]tomcat页面出错,请注意......状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"
echo "[error]页面访问出错,开始重启tomcat"
kill -9 $TomcatID # 杀掉原tomcat进程
sleep 3
rm -rf $TomcatCache # 清理tomcat缓存
$StartTomcat
fi
else
echo "[error]tomcat进程不存在!tomcat开始自动重启..."
echo "[info]$StartTomcat,请稍候......"
rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor >>$TomcatMonitorLog
# 获取tomcat进程ID
TomcatID=$(ps -ef |grep tomcat |grep -w 'tomcat_2'|grep -v 'grep'|awk '{print $2}')
# tomcat启动程序(这里注意tomcat实际安装的路径)
StartTomcat=/opt/aiwm/tomcat/tomcat_2/bin/startup.sh
TomcatCache=/opt/aiwm/tomcat/tomcat_2/bin/work
# 定义要监控的页面地址
WebUrl=http://localhost:8081/DocumentPreviewDemo/
# 日志输出
GetPageInfo=/opt/aiwm/tomcat/tomcat_2/logs/TomcatMonitor.Info
TomcatMonitorLog=/opt/aiwm/tomcat/tomcat_2/logs/TomcatMonitor.log
Monitor()
{
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
if [ $TomcatID ];then # 这里判断TOMCAT进程是否存在
echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..."
# 检测是否启动成功(成功的话页面会返回状态"200")
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常......"
else
echo "[error]tomcat页面出错,请注意......状态码为$TomcatServiceCode,错误日志已输出到$GetPageIn
fo"
echo "[error]页面访问出错,开始重启tomcat"
kill -9 $TomcatID # 杀掉原tomcat进程
sleep 3
rm -rf $TomcatCache # 清理tomcat缓存
$StartTomcat
fi
else
echo "[error]tomcat进程不存在!tomcat开始自动重启..."
echo "[info]$StartTomcat,请稍候......"
rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor >>$TomcatMonitorLog
测试OK后把脚本加入到crontab定时执行任务 crontab -e * * * * * sh /opt/aiwm/tomcat/tomcat.sh > /dev/null 2>&1 /dev/null 2>&1 不输出信件