---
title: "排查占用资源java代码脚本"
author: "alwaysnew"
date: "2016-11-12T20:44:27Z"
tags: []
categories: ["互联网"]
slug: "java"
draft: false
lastmod: "2016-11-12T20:44:27Z"
id: 2337537
url: "https://alwaysnew.iteye.com/blog/2337537"
---

show-busy-java-threads.sh（摘自：https://github.com/oldratlee/useful-scripts/blob/master/show-busy-java-threads.sh）

 

 

#!/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       readonly PROG=\`basename $0\`   readonly -a COMMAND\_LINE=("$0" "$@")       usage() {   cat &lt;&lt;EOF   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   }       readonly ARGS=\`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"\`   \[ $? -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 "$@"   echo -e "\\033\[0m"   } || echo "$@"   }       yellowEcho() {   \[ -c /dev/stdout ] && {   # if stdout is console, turn on color output.   echo -ne "\\033\[1;33m"   echo -n "$@"   echo -e "\\033\[0m"   } || echo "$@"   }       blueEcho() {   \[ -c /dev/stdout ] && {   # if stdout is console, turn on color output.   echo -ne "\\033\[1;36m"   echo -n "$@"   echo -e "\\033\[0m"   } || echo "$@"   }       # Check the existence of jstack command!   if ! which jstack &&gt; /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       readonly uuid=\`date +%s\`\_${RANDOM}\_$$       cleanupWhenExit() {   rm /tmp/${uuid}\_* &&gt; /dev/null   }   trap "cleanupWhenExit" EXIT       printStackOfThreads() {   local line   local count=1   while IFS=" " read -a line ; do   local pid=${line\[0]}   local threadId=${line\[1]}   local threadId0x="0x\`printf %x ${threadId}\`"   local user=${line\[2]}   local pcpu=${line\[4]}       local jstackFile=/tmp/${uuid}\_${pid}       \[ ! -f "${jstackFile}" ] && {   {   if \[ "${user}" == "${USER}" ]; then   jstack ${pid} &gt; ${jstackFile}   else   if \[ $UID == 0 ]; then   sudo -u ${user} jstack ${pid} &gt; ${jstackFile}   else   redEcho "\[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."   redEcho "User of java process($user) is not current user($USER), need sudo to run again:"   yellowEcho " sudo ${COMMAND\_LINE\[@]}"   echo   continue   fi   fi   } || {   redEcho "\[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."   echo   rm ${jstackFile}   continue   }   }   blueEcho "\[$((count++))] Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user}):"   sed "/nid=${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}" | printStackOfThreads