首页 > 代码库 > 一个根据内存使用情况重启tomcat的小脚本
一个根据内存使用情况重启tomcat的小脚本
有一台服务器上部署了tomcat,随着时间的推移,内存消耗越来越大,所以写了一个小脚本配合crontab定时检测内存,不足的情况下重启tomcat。
#!/bin/bash
#tomcat restart:out of memory
LOG=./restart_tomcat.log
TOMCAT=/tomcat/bin/
TOMCATDIR=/tomcat
echo"----------------------------" >> $LOG
mem_total=$(free -m |grep Mem|awk ‘{print$2}‘)
mem_used=$(free -m|grep -|awk ‘{print $3}‘)
mem_rate=$(echo "scale=4;$mem_used /$mem_total" | bc)
percent_part1=$(echo $mem_rate | cut -c2-3)
percent_part2=$(echo $mem_rate | cut -c4-5)
/usr/bin/free -m >>$LOG
echo "System memery is already use:$percent_part1.$percent_part2%"| tee -a $LOG
if (( percent_part1 >= 90 ));
then
echo"----------------------------" >> $LOG;
date | tee -a $LOG;
bash$TOMCATDIR/checktomcat | tee -a $LOG;
sleep5;
echo"----------------------------" >> $LOG;
date | tee -a $LOG;
echo"kill java" >>$LOG;
killall java | tee -a $LOG;
sleep5;
echo"----------------------------" >> $LOG;
netstat-anptul|grep :80 |grep LISTEN| tee -a $LOG;
echo"----------------------------" >> $LOG;
date | tee -a $LOG;
echo"start tomcat" >>$LOG;
bash$TOMCAT/startup.sh | tee -a $LOG;
sleep10;
echo"----------------------------" >> $LOG;
date | tee -a $LOG;
bash$TOMCATDIR/checktomcat | tee -a $LOG;
netstat-anptul|grep :80 |grep LISTEN| tee -a $LOG;
sleep5;
else
date>>$LOG;
echo"The server is very good."| tee -a $LOG;
fi;
exit 0
本文出自 “拔电源的运维空间” 博客,请务必保留此出处http://zhangdj.blog.51cto.com/9210512/1880090
一个根据内存使用情况重启tomcat的小脚本