132

I am using kill -3 command to see the JVM's thread dump in unix. But where can I find the output of this kill command? I am lost!!

AlexElin
  • 1,044
  • 14
  • 23
javanerd
  • 2,802
  • 4
  • 24
  • 32

10 Answers10

217

You could alternatively use jstack (Included with JDK) to take a thread dump and write the output wherever you want. Is that not available in a unix environment?

jstack PID > outfile
Joshua McKinnon
  • 24,489
  • 11
  • 57
  • 63
  • Thanks for that.Absolutely!! Yes I can use this. jstack PID > outfile will output the thread dump at that particular period of time. Isn't it? – javanerd Feb 02 '11 at 16:47
  • 1
    Yes - at the point in time it is run. You can also specify -l (lowercase L) for a long listing that prints additional lock information – Joshua McKinnon Feb 02 '11 at 18:44
  • 2
    Until the jstack command fails consistently due to "Unable to deduce type of thread from address" ;-( – noahlz Nov 04 '13 at 18:54
  • 1
    If you're seeing that error, I suggest taking it up with your vendor. A quick search shows, for example, there is an open bug in RHEL regarding this error and openjdk... – Joshua McKinnon Nov 04 '13 at 19:46
  • 7
    It's worth noting that jstack requires the JDK. If you're running apps on a server that only has the JRE installed, you'll need to find another means for thread dumping. – jeffkempf Nov 27 '17 at 15:30
  • 1
    Here is how to use jstack to obtain thread dump of a process running under different user, like windows service: https://stackoverflow.com/questions/1197912/cant-debug-java-windows-services-with-jhat-jps-jstack/47723393#47723393 – Vadzim Dec 11 '17 at 11:42
  • I found this didn't result in the dump being sent to the STDOUT of jstack but the STDOUT of the java process instead, possibly because withtout the `-F` flag the request failed, then adding the `-F` may have caused the process to do this. – JinnKo Jul 12 '18 at 11:28
48

The thread dump is written to the system out of the VM on which you executed the kill -3. If you are redirecting the console output of the JVM to a file, the thread dump will be in that file. If the JVM is running in an open console, then the thread dump will be displayed in its console.

slm
  • 15,396
  • 12
  • 109
  • 124
Kris Babic
  • 6,254
  • 1
  • 29
  • 18
34

There is a way to redirect JVM thread dump output on break signal to separate file with LogVMOutput diagnostic option:

-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=jvm.log
Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • 6
    Technically this doesn't "redirect" the thread dump output. It turns on JVM logging into jvm.log (which includes thread dump output) but kill -QUIT will still dump to the process's stdout (aswell). Upvoted for the description of obscure JVM options :) – sqweek Feb 24 '14 at 10:12
29

With Java 8 in picture, jcmd is the preferred approach.

jcmd <PID> Thread.print

Following is the snippet from Oracle documentation :

The release of JDK 8 introduced Java Mission Control, Java Flight Recorder, and jcmd utility for diagnosing problems with JVM and Java applications. It is suggested to use the latest utility, jcmd instead of the previous jstack utility for enhanced diagnostics and reduced performance overhead.

However, shipping this with the application may be licensing implications which I am not sure.

Arnab Biswas
  • 4,495
  • 3
  • 42
  • 60
  • 2
    Unfortunately `jcmd` fails to connect to windows service process with `com.sun.tools.attach.AttachNotSupportedException: Insufficient memory or insufficient privileges to attach` while `jstack -F` succeeds: https://stackoverflow.com/questions/1197912/cant-debug-java-windows-services-with-jhat-jps-jstack/47723393#47723393 – Vadzim Dec 11 '17 at 11:41
  • 4
    You need to run jcmd Thread.dump under the same user as java process has, otherwise your connections will be dropped. See https://stackoverflow.com/questions/25438983/com-sun-tools-attach-attachnotsupportedexception-unable-to-open-socket-file-ta – Twilite May 21 '19 at 10:32
14

In the same location where the JVM's stdout is placed. If you have a Tomcat server, this will be the catalina_(date).out file.

Daniel
  • 27,718
  • 20
  • 89
  • 133
9

When using kill -3 one should see the thread dump in the standard output. Most of the application servers write the standard output to a separate file. You should find it there when using kill -3. There are multiple ways of getting thread dumps:

  • kill -3 <PID>: Gives output to standard output.
  • If one has access to the console window where server is running, one can use Ctrl+Break combination of keys to generate the stack trace on STDOUT.
  • For hotspot VM's we can also use jstack command to generate a thread dump. It’s a part of the JDK. Syntax is as follows:

    Usage:
    
    jstack [-l] <pid> (to connect to running process)
    jstack -F [-m] [-l] <pid>(to connect to a hung process)
    
     - For JRockit JVM we can use JRCMD command which comes with JDK Syntax: 
       jrcmd <jrockit pid> [<command> [<arguments>]] [-l] [-f file] [-p] -h]
    
slm
  • 15,396
  • 12
  • 109
  • 124
Apoorve
  • 165
  • 3
  • 9
  • I'm having trouble using Kill -3 . It works ok but kills the process also after writing thread dump to console. Is it supposed to do that? – Ashley May 09 '18 at 15:44
  • 1
    @Ashley - no `kill -3 ` shouldn't kill the JVM. What type of Java app are you looking at? – slm Nov 08 '19 at 00:12
2

In Jboss you can perform the following

nohup $JBOSS_HOME/bin/run.sh -c  yourinstancename $JBOSS_OPTS >> console-$(date +%Y%m%d).out  2>&1 < /dev/null &
kill -3 <java_pid>

This will redirect your output/threadump to the file console specified in the above command.

anish
  • 6,884
  • 13
  • 74
  • 140
2
  1. Find the process id [PS ID]
  2. Execute jcmd [PS ID] Thread.print
Mehmet Erdemsoy
  • 117
  • 1
  • 3
2

Steps that you should follow if you want the thread dump of your StandAlone Java Process

Step 1: Get the Process ID for the shell script calling the java program

linux$ ps -aef | grep "runABCD"

user1  **8535**  4369   0   Mar 25 ?           0:00 /bin/csh /home/user1/runABCD.sh

user1 17796 17372   0 08:15:41 pts/49      0:00 grep runABCD

Step 2: Get the Process ID for the Child which was Invoked by the runABCD. Use the above PID to get the childs.

linux$ ps -aef | grep **8535**

user1  **8536**  8535   0   Mar 25 ?         126:38 /apps/java/jdk/sun4/SunOS5/1.6.0_16/bin/java -cp /home/user1/XYZServer

user1  8535  4369   0   Mar 25 ?           0:00 /bin/csh /home/user1/runABCD.sh

user1 17977 17372   0 08:15:49 pts/49      0:00 grep 8535

Step 3: Get the JSTACK for the particular process. Get the Process id of your XYSServer process. i.e. 8536

linux$ jstack **8536** > threadDump.log
Tim
  • 41,901
  • 18
  • 127
  • 145
Nick
  • 21
  • 3
0

For example, if you use tomcat. The log possible will stay at [tomcat dir]/logs/catalina.out