#!/bin/bash if [ -z "$1" ]; then ### 1.先找到消耗cpu最高的Java进程 ### pid=`ps -eo pid,%cpu,cmd --sort=-%cpu | grep java | grep -v grep | head -1 | awk 'END{print $1}' ` if [ "$pid" = "" ]; then echo "无Java进程,退出。" exit fi else pid=$1 fi curTime=$(date +%Y%m%dT%H%M%S) dumpFilePath="./pid$pid−$curTime.jstack" echo -e "java 进程ID为 $pid" > $dumpFilePath ps -ef|grep $pid >> $dumpFilePath topThreadId=`top -b -n 1 -Hp $pid|grep java|head -n 1|awk '{print $1}'` cpuUsage=`top -b -n 1 -Hp $pid|grep java|head -n 1|awk '{print $9}'` echo -e "最耗cpu的使用率为 $cpuUsage">>$dumpFilePath topThreadId16=`printf "%x" $topThreadId` topThreadId16ThreadName="nid=0x$topThreadId16" echo -e "最耗cpu的java线程ID 16进制为 $topThreadId16ThreadName">>$dumpFilePath jstack -l $pid >>$dumpFilePath #threadDe -e tail=`jstack ${pid}|grep ${topThreadId16} -C 10` #echo "$threadDetail
标签:grep,java,pid,echo,dumpFilePath,查找,Java,cpu From: https://www.cnblogs.com/wujf/p/17326755.html