rickygrimes
rickygrimes

Reputation: 2716

Taking thread dump using jstack

I would like to generate an automatic thread dump. This is the script I am using:

    THREADDUMP_LOG_FILE="/tmp/tmpLog_`date +"%Y%m%d%H%M%S"`"
    pid=`ps axww | grep -v grep | sed -e 's/^[ ]*//g' | sed -e 's/[ ][ ]*/ /g' | cut -f1 -d' ' `
    $JAVA_HOME/bin/jstack $pid >> $THREADDUMP_LOG_FILE

When I do this, this is what I see in the thread dump log file:

Usage:
jstack [-l] <pid>
    (to connect to running process)
jstack -F [-m] [-l] <pid>
    (to connect to a hung process)
jstack [-m] [-l] <executable> <core>
    (to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
    (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

Clearly, I am doing something wrong. How can I take a thread dump using jstack?

Upvotes: 1

Views: 3566

Answers (2)

Kuro
Kuro

Reputation: 321

You can't give more than one pid to jstack, and you should give it a pid of java process only. Something like this should work:

for pid in $(ps axww | grep ' java' |grep -v grep | sed -e 's/^[ ]*//g' | sed -e 's/[ ][ ]*/ /g' | cut -f1 -d' '); do
  jstack $pid >> $THREADDUMP_LOG_FILE
done

Or more simply:

for pid in $(jps | sed 's/^\([0-9][0-9]*\) .*$/\1/'); do
  jstack $pid >> $THREADDUMP_LOG_FILE
done

Upvotes: 0

Dieter
Dieter

Reputation: 311

I have no direct answer, but I assume that the shell-foo-magic you do to get the PID is perhaps wrong. Therefore could you please add an 'echo $PID' directly after the pid=... line and showus the output. Because useing directly 'jstack ' works fine for me.

Upvotes: 1

Related Questions