Skip to content

Instantly share code, notes, and snippets.

@noodles-v6
Forked from sdslnmd/top.sh
Created August 18, 2013 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noodles-v6/6261492 to your computer and use it in GitHub Desktop.
Save noodles-v6/6261492 to your computer and use it in GitHub Desktop.
#!/bin/bash
redEcho()
{
echo -e "\033[1;31m$@\033[0m"
}
pid=`pgrep java`
uuid=`date +%s`_${RANDOM}_$$
jstackFile=/tmp/${uuid}_${pid}
sudo -u tomcat jstack ${pid} > ${jstackFile}
top -b -n1 -d0.01 -H -p ${pid} | grep tomcat | sort -k9 -r -n | head -5 | while read threadLine ; do
threadId=`echo ${threadLine} | awk '{print $1}'`
threadId0x=`printf %x ${threadId}`
pcpu=`echo ${threadLine} | awk '{print $9}'`
user=`echo ${threadLine} | awk '{print $2}'`
[ ! -f "${jstackFile}" ] &&
{
{ redEcho "Fail to jstack java process ${pid}"; rm ${jstackFile} ; continue; }
}
redEcho "The stack of busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) of java process(${pid} of user(${user}):"
sed "/nid=0x${threadId0x}/,/^$/p" -n ${jstackFile}
done
rm /tmp/${uuid}_*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment