您的位置:首页 > 运维架构 > Tomcat

监控多实例tomcat的cpu使用率

2016-05-20 14:18 761 查看
需求:

监控top命今令下,tomcat的进程%cpu时间占比超过80%时候 kill掉进程,再重启对应该的tomcat,并且记录一下重启的tomcat,还有重启时间的日志

#!/bin/bash

#打印tomcatID号
#tomcatid=`ps -ef |grep tomcat|grep -w 'tomcat' |grep -v grep|awk '{print $2}'`
tomcatid=`jps |grep Bootstrap|awk '{print $1}'`
DATETIME=`(date '+%F %H:%M:%S')`  #获取单前时间
LOG=/tmp/tomcat.log  #日志文件
if [ ! -n "$tomcatid" ];then
echo "Please start tomcat." #判断pid是否为空
else

for i in $tomcatid;do

tomcatdir=` ps aux|grep $i |awk '{for (i=1;i<NF;i++) if($i ~ /Dcatalina\.home/) print $i}'|awk -F '=' '{print $2}'|head -1` #获取tomcat的catalina home路径
if [ "$i" ];then
echo "The Tomcat is Starting. PID: $i. TomcatDir:$tomcatdir"
#通过pid判断tomcat是否启动;已启动判断cpu
cpu=`top -bn1 |grep "$i" | awk '{print $9}'|cut -d "." -f1`
if [ $cpu -lt 80 ];then
kill -9 "$i"
sleep 3
"$tomcatdir/bin/startup.sh"
echo  "The Tomcat Pid : $i   Restart Tomcat Time $DATETIME" >> $LOG

fi

fi
done
fi
~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  监控 shell