Skip to content

Instantly share code, notes, and snippets.

@johntbush
Created September 22, 2016 21:55
Show Gist options
  • Select an option

  • Save johntbush/2980b8b09c52ad0cedaa522d791285ae to your computer and use it in GitHub Desktop.

Select an option

Save johntbush/2980b8b09c52ad0cedaa522d791285ae to your computer and use it in GitHub Desktop.
thread_dump
#!/bin/bash
# manually create a heap dump file for tomcat using jmap
. /etc/profile.d/common_shell_functions
get_tomcat_pid() {
PID=$(service tomcat status | awk {'print $3'} | sed 's/)//g')
[[ -z $PID ]] && die "Unable to determine tocat pid; does not appear to be running"
}
# main
get_tomcat_pid
TOMCAT_DIR=<%= scope.function_hiera(["app_basedir"]) %>/tomcat
HEAPDUMP_FILE="manual_pid${PID}_$(date +%Y%m%d%H%M%S).hprof"
RUNAS='sudo -u rsmart'
cd $TOMCAT_DIR || die "Unable to go to $TOMCAT_DIR"
$RUNAS jmap -dump:format=b,file=${HEAPDUMP_FILE} $PID
[[ $? -ne 0 ]] && die "Unable to create heap dump file $HEAPDUMP_FILE"
$RUNAS chmod 644 $HEAPDUMP_FILE
[[ $? -ne 0 ]] && die "Unable to chmod ${HEAPDUMP_FILE}"
echo "Compressing the following heap dump file:"
ls -lGh $HEAPDUMP_FILE
$RUNAS pbzip2 -l $HEAPDUMP_FILE
[[ $? -ne 0 ]] && die "Unable to pbzip -l ${HEAPDUMP_FILE}"
echo "Created heap dump file ${TOMCAT_DIR}/${HEAPDUMP_FILE}.bz2 for tomcat PID $PID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment