Shell Script For Jmap Heap Dump

Shell Scripting

Taking heap dump might be sometimes tricky but if we schedule it through cronjob and but it will be much easier via a shell script. Here we will discuss about creating a shell script for jmap heap dump. jmap is a very effective tool for on demand heap dump generation. Here the heapdump and thread dump is taken in weblogic server. With little modification the same script can be used for taking heap dumps in other servers too if you know how to grep for the java process ID.


ADVERTISEMENT

Note: If you are looking for Jmap heap dump analysis then head over to this heap dump analysis post.

Below script takes heap dump and thread dumps in intervals. You can schedule it to take dumps using crontab.

Note: Here we have created the jmap heap dump shell script for weblogic server. You can modify the script to suit your needs.

 #!/bin/bash
#Weblogic Domain root dir
WL_DOMAIN="/opt/bea/domains/beaprod"

JAVA_BIN="/opt/bea/jdk1.5.0_15/bin"

#MS_NAME=Manage server Name
MS_NAME=`grep "^SERVER_NAME" $WL_DOMAIN/bin/setDomainEnv.sh |awk -F";" '{print $1}' |awk -F"=" '{print $NF}'`

LOG_DIR="$WL_DOMAIN/servers/$MS_NAME/logs"
#Script output log directory
SCRIPT_LOG="$LOG_DIR/heap_dumps.log"
#Get the managed server process ID
MS_PID=`ps -auxw |grep java |grep $MS_NAME |awk '{print $2}'`
echo "`date +%x" "%X` Starting Heap Dump $DUMP_COUNT" >>$SCRIPT_LOG
$JAVA_BIN/jmap -heap:format=b $MS_PID >>$SCRIPT_LOG 2>&1
#Move the heap dump and keep it with creation dates to recognize the time when the dump taken
mv heap.bin $LOG_DIR/heap_$MS_NAME`date +%m%d%Y_%H%M`.bin
echo "`date +%x" "%X` Completed Heap Dump $DUMP_COUNT" >>$SCRIPT_LOG
#Take 4 thread dumps after heap dump
for i in 1 2 3 4; do
kill -3 $MS_PID
sleep 60
done

If interested you can read more about JMAP here.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply