lfjigu 发表于 2018-12-7 10:04:43

一个根据内存使用情况重启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;
  killalljava | 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
  




页: [1]
查看完整版本: 一个根据内存使用情况重启tomcat的小脚本