Tomcat 日志监控自动重启脚本

阅读更多
因项目经常出现一些异常,导致接口挂死,定时检测日志内容进行重启。

tomcat-monitor.sh

#!/bin/sh
tomcat_path=/home/apache-tomcat-6.0.32
tomcat_log=$tomcat_path/logs/tomcat_fatal_error.log
ram_max=1
err_content1='java.lang.OutOfMemoryError'
err_content2='OALL8 is in an inconsistent state'
err_content3='java.sql.SQLException: Protocol violation'
err_content4='java.net.SocketException: Broken pipe'
pid_ram=`ps -ef | grep $tomcat_path|grep -v grep|grep -v tomcat-monitor.sh|awk '{print $2}'|xargs|awk '{print $1}'`

#echo $pid_ram   >>$tomcat_log
errs=`cat $tomcat_path/logs/catalina.out|grep -E "($err_content1|$err_content2|$err_content3|$err_content4)" |wc -l`
#echo $errs
curDate=`date +"%Y%m%d%H%M%S"`
newfile=$tomcat_path/logs/catalina.out_$curDate
#echo $tomcat_path
#echo $errs
if expr $errs \>= $ram_max > /dev/null
then
echo "`date "+%F %T %A"`  fatal error" >>$tomcat_log
if [ $pid_ram ]; then
  kill -9 $pid_ram
fi

cp $tomcat_path/logs/catalina.out  $newfile
> $tomcat_path/logs/catalina.out
#sleep 10
$tomcat_path/bin/startup.sh   >>$tomcat_log
echo "`date "+%F %T %A"` tomcat is start.$SECONDS s" >>$tomcat_log
else
#cp $tomcat_path/logs/catalina.out  $newfile
#> $tomcat_path/logs/catalina.out
echo "`date "+%F %T %A"` tomcat is running.$SECONDS s" >>$tomcat_log
fi

你可能感兴趣的:(tomcat监控)