Friday, March 11, 2011

Redirect thread dump to another file?

On 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. :-)

Here are some explanations about frequently used  linux cmd > /dev/null 2>&1

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR. Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. The default is STDOUT if you don't name or number.

That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.

You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

Note: I somehow lost the original link to the post which includes above explanation. I copied here as it is very clear and easy to understand. Thanks the original poster for contribution.

2 comments:

  1. Hi Jim,

    Thanks for the sharing the information...

    i have a doubt, using jstack command can we redirect the thread dump to separate file or it is redirecting to the stdout.

    if yes, can you please share the command to redirect.

    thanks,
    Learning the new things

    ReplyDelete
  2. Hi Sumanth,
    On Linux, I believe you can use one of below commands to redirect (-F option is to force dump):

    jstack pid > jstack.out
    jstack -F pid > jstack.out

    ReplyDelete