Skip to content

Instantly share code, notes, and snippets.

@sdslnmd
Created July 23, 2013 08:19
Show Gist options
  • Save sdslnmd/6060738 to your computer and use it in GitHub Desktop.
Save sdslnmd/6060738 to your computer and use it in GitHub Desktop.

Revisions

  1. sdslnmd created this gist Jul 23, 2013.
    28 changes: 28 additions & 0 deletions top.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/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}_*