标签:shell 监控
需求:
监控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 ~
本文出自 “时光依然轻擦” 博客,请务必保留此出处http://xulianglinux.blog.51cto.com/8001428/1775388
标签:shell 监控
原文地址:http://xulianglinux.blog.51cto.com/8001428/1775388