您的位置:首页 > 编程语言 > Java开发

kill -3无法输出java堆栈信息,使用jstack

2013-04-12 11:26 761 查看

Redirect thread dump to another file?

On Jboss or Tomcat application server, we usually use kill -3 PID to get thread dump to default STDOUT which is catalina.out under $Tomcat_Home/logs folder. It might be nature to use command
kill -3 PID > some.file 2>&1 to try to redirect the thread dump info to some.file than default one. However, it will not work. The reason is kill is just a command to send a signal to a process. You are redirecting the output of the kill command
itself rather than the process (what the process does upon receipt of a signal is separate), so the redirect (supposed to kill command itself) has no effect on which file the process (PID) will write to. Given that, if we need redirect thread dump for that
process to some other file, we need add redirects to that process when it starts.

Another popular way is to use jstack -F PID to get the whole thread dump forcefully."jstack": A JVM troubleshooting tool that prints stack traces of all running threads of a given JVM process, a Java core file, or remote debug
server. It comes with
JDK so it is free too. :-)

jstack

If installed/available, we recommend using the jstack tool. It prints thread dumps to the command line console.

To obtain a thread dump using jstack, run the following command:

jstack

You can output consecutive thread dumps to a file by using the console output redirect/append directive:

jstack >> threaddumps.log

jstack script

Here's a script, taken from
eclipse.org that will take a series of thread dumps using jstack.

#!/bin/bash

if [ $# -eq 0 ]; then

echo >&2 "Usage: jstackSeries [ [ ] ]"

echo >&2 " Defaults: count = 10, delay = 0.5 (seconds)"

exit 1

fi

pid=$1 # required

user=$2 # required

count=${3:-10} # defaults to 10 times

delay=${4:-0.5} # defaults to 0.5 seconds

while [ $count -gt 0 ]

do

sudo -u $user jstack -l $pid >jstack.$pid.$(date +%H%M%S.%N)

sleep $delay

let count--

echo -n "."

done

Just run it like this:

sh jstackSeries.sh [pid] [cq5serveruser] [count] [delay]

For example:

sh jstackSeries.sh 1234 cq5serveruser 10 3
http://linuxhelp4u.blogspot.com/2011/04/redirect-thread-dump-to-another-file.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: