java程序故障排查脚本之——CPU占用高

时间:2021-07-28 03:31:37

root@ubuntu-B85M-D3H:~/tmp# cat java_Analy.sh

#!/bin/bash

T=`ps -mp $1 -o THREAD,tid,time|sort -k 2 -nr|awk '{print $2","$8","$9}'|head -n 11|grep -v "-"`

for i in $T
do
consum=`echo $i |awk -F"," '{print $1}'`
tid=`echo $i |awk -F"," '{print $2}'`
dtime=`echo $i |awk -F"," '{print $3}'`
id=`printf "%x\n" $tid`
echo "线程消耗CPU:$consum% 运行时间:$dtime 线程id:$id" >>Analy.txt
jstack $1 |grep $id -A 30 >>Analy.txt
echo "">>Analy.txt
echo "===========================================================================================" >>Analy.txt

done

使用说明:先使用top查询出占用cpu高的java程序的pid,然后执行./java_Analy.sh pid  之后把生成的Analy.txt文件交给开发就行了

参考链接:http://blog.csdn.net/blade2001/article/details/9065985