需求: tomcat
監控top命今令下,tomcat的進程%cpu時間佔比超過80%時候 kill掉進程,再重啓對應該的tomcat,而且記錄一下重啓的tomcat,還有重啓時間的日誌 bash
#!/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 ~