How to take a Java thread dump from a shell/bash script

We already know how to get a Java thread dump manually. But what if we want to take a Java thread dump from a script to help us automate things.

To do this we need to come up with a ps and grep combination that will only return one result. so grep as many times as neccessary and/or use keywords that are only found in your process name so you only get one result.

Remember to add [] somewhere in your first grep so grep doesn't matches itself.
output=$(ps -ef | grep m[y]ProcesseName )
Then all you need to do is sending the QUIT signal to $2 which stores the PID of your process.
kill -QUIT $2
So just add the above two lines to your script after modifying the first line to fit your needs and you should be good to go.

No comments:

Post a Comment