Jstack is one of the most important tools when trying to figure out what a java process is doing apart from looking at the logs. It has to be used in conjunction with jps in order to give it a process id. It shows a list of threads, each one has a name, and they appear in the order that they were created.
In order to take a jstack you can perform following steps-
Step 1: Get the PID of your java process
# ps -elf | grep java_process
Step 2: Request a Thread Dump from the JVM. Assuming jstack is in the path
# jstack -F <procees_id> > jstack1.out
wait 5 minutes and take another one
# jstack -F <procees_id> > jstack2.out
Script for collecting jstack
#!/bin/sh
#
# Takes the PID as an argument.
# Make sure you set JAVA_HOME
#
# Create thread dumps a specified number of times (i.e. LOOP) and INTERVAL.
#
# Thread dumps will be collected in the file "jstack_threaddump.out", in the same directory from where this script is been executed.
#
# Usage: sh ./threaddump_linux_jstack-continuous.sh <JBOSS_PID>
#
# Number of times to collect data.
LOOP=6
# Interval in seconds between data points.
INTERVAL=20
# Setting the Java Home, by giving the path where your JDK is kept
JAVA_HOME=/home/jdk1.6.0_21
for ((i=1; i <= $LOOP; i++))
do
$JAVA_HOME/bin/jstack -l $1 >> jstack_threaddump.out
echo "thread dump #" $i
if [ $i -lt $LOOP ]; then
echo "sleeping..."
sleep $INTERVAL
fi
done