查找cpu占用高的函数for java
#!/bin/sh
#echo "@查找到的进程信息:"
#ps -ef|grep $1|grep -v grep|grep -v jstack
cpuid=`ps -ef|grep $1|grep -v grep|grep -v jstack|awk '{print $2}'`
#echo "-------------------------------------------------------"
#echo "@占用最高的子进程:"
#ps -mp $cpuid -o THREAD,tid,time|sort -nrk2|sed -n '2p'
cpubuy=`ps -mp $cpuid -o THREAD,tid,time|sort -nrk2|sed -n '2p'|awk '{print $2}'`
if [[ $cpubuy > 75 ]] ;then
tid=`ps -mp $cpuid -o THREAD,tid,time|sort -nrk2|sed -n '2p'|awk '{print $8}'`
# echo "-------------------------------------------------------"
# echo "@将子进程转换为16进制:"
if [[ $tid != "-" ]];then
printf "%x\n" $tid
slid=`printf "%x\n" $tid`
echo "-------------------------------------------------------"
echo "@资源占用最高的函数:"
jstack $cpuid|grep $slid -A 30
fi
fi
脚本保存为jstack.sh
执行时脚本后跟着关键字
jstack.sh tomcat