如果CPU占用率持續(xù)達(dá)到80以上則調(diào)用打印java線(xiàn)程占用率堆棧的腳本,見(jiàn)https://github.com/oldratlee/useful-shells/blob/master/show-busy-java-threads.sh,具體用法見(jiàn)他的github wiki!
#!/bin/bash #cpu idle percent cpu_idle=`top -b -d 1 -n 2 | grep Cpu | awk 'NR>1{print $5}' | cut -f 1 -d "."` if [ -d "/opt/scripts/log" ] then cd /opt/scripts else mkdir /opt/scripts/log fi count=0 condition=true while condition do if [ $cpu_idle -le 20 ] then pids=`top -b -d 1 -n 1 | grep java | awk '{if($9 > 80) print $1}'` for ((a = 1; a 10; a++)) do for pid in $pids doi #involve jdk existence check later date=`date +%Y%m%d%H%M` sh showstack.sh -c 30 -p $pid > log/stack_$pid_$date.log jstat -gcutil $pid > log/gc_$pid_$date.log done done for pid in $pids do date=`date +%Y%m%d%H%M` jmap -heap $pid > log/heap_$pid_$date.log done count++ cpu_idle=`top -b -d 1 -n 2 | grep Cpu | awk 'NR>1{print $5}' | cut -f 1 -d "."` if [ $count -ge 3] then #send emaili #server ip address ip=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` condition=false count=0 fi else condition=false count=0 fi done
以上所述就是本文給大家分享的全部?jī)?nèi)容了,希望大家能夠喜歡。
標(biāo)簽:仙桃 江西 大慶 遵義 上饒 三門(mén)峽 安康 大興安嶺
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux shell腳本實(shí)現(xiàn)CPU預(yù)警》,本文關(guān)鍵詞 Linux,shell,腳本,實(shí)現(xiàn),CPU,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。