java进程cpu使用率高_shell 分析java进程cpu使用率过高的shell脚本

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

#!/bin/bash

# @Function

# Find out the highest cpu consumed threads of java,and print the stack of these threads.

#

# @Usage

# $ ./show-busy-java-threads.sh

#

# @author Jerry Lee

PROG=`basename $0`

usage() {

cat <

Usage: ${PROG} [OPTION]...

Find out the highest cpu consumed threads of java,and print the stack of these threads.

Example: ${PROG} -c 10

Options:

-p,--pid find out the highest cpu consumed threads from the specifed java process,default from all java process.

-c,--count set the thread count to show,default is 5

-h,--help display this help and exit

EOF

exit $1

}

ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "[email protected]"`

[ $? -ne 0 ] && usage 1

eval set -- "${ARGS}"

while true; do

case "$1" in

-c|--count)

count="$2"

shift 2

;;

-p|--pid)

pid="$2"

shift 2

;;

-h|--help)

usage

;;

--)

shift

break

;;

esac

done

count=${count:-5}

redEcho() {

[ -c /dev/stdout ] && {

# if stdout is console,turn on color output.

echo -ne "\033[1;31m"

echo -n "[email protected]"

echo -e "\033[0m"

} || echo "[email protected]"

}

# Check the existence of jstack command!

if ! which jstack &> /dev/null; then

[ -z "$JAVA_HOME" ] && {

redEcho "Error: jstack not found on PATH!"

exit 1

}

! [ -f "$JAVA_HOME/bin/jstack" ] && {

redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"

exit 1

}

! [ -x "$JAVA_HOME/bin/jstack" ] && {

redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"

exit 1

}

export PATH="$JAVA_HOME/bin:$PATH"

fi

uuid=`date +%s`_${RANDOM}_$$

cleanupWhenExit() {

rm /tmp/${uuid}_* &> /dev/null

}

trap "cleanupWhenExit" EXIT

printStackOfThread() {

while read threadLine ; do

pid=`echo ${threadLine} | awk '{print $1}'`

threadId=`echo ${threadLine} | awk '{print $2}'`

threadId0x=`printf %x ${threadId}`

user=`echo ${threadLine} | awk '{print $3}'`

pcpu=`echo ${threadLine} | awk '{print $5}'`

jstackFile=/tmp/${uuid}_${pid}

[ ! -f "${jstackFile}" ] && {

jstack ${pid} > ${jstackFile} || {

redEcho "Fail to jstack java process ${pid}!"

rm ${jstackFile}

continue

}

}

redEcho "Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"

sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}

done

}

ps -Leo pid,lwp,user,comm,pcpu --no-headers | {

[ -z "${pid}" ] &&

awk '$4=="java"{print $0}' ||

awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'

} | sort -k5 -r -n | head --lines "${count}" | printStackOfThread

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值