Skip to content

Instantly share code, notes, and snippets.

@padusumilli
Created January 14, 2025 07:15
Show Gist options
  • Save padusumilli/c3a8d7a3094f926df03b5e5831ad721c to your computer and use it in GitHub Desktop.
Save padusumilli/c3a8d7a3094f926df03b5e5831ad721c to your computer and use it in GitHub Desktop.
Script to take a memory dump every 4 hours
#!/bin/bash
# Set variables
JAVA_PID=$1 # Java process ID (pass as an argument to the script)
TASK_NAME=$2
DUMP_DIR="/tmp/TASK_NAME" # Directory to store dumps temporarily
S3_BUCKET="s3://com.myvipre.tcpdump.myvpv3"
# Ensure the dump directory exists
mkdir -p "$DUMP_DIR"
# Function to take memory dump and upload to S3
take_memory_dump() {
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
DUMP_FILE="heapdump_${JAVA_PID}_${TIMESTAMP}.hprof"
DUMP_PATH="$DUMP_DIR/$DUMP_FILE"
echo "Taking memory dump of PID $JAVA_PID at $TIMESTAMP..."
jmap -dump:live,format=b,file="$DUMP_PATH" "$JAVA_PID"
if [ $? -eq 0 ]; then
echo "Memory dump successful. Uploading $DUMP_FILE to S3..."
aws s3 cp "$DUMP_PATH" "$S3_BUCKET/"
if [ $? -eq 0 ]; then
echo "Upload successful. Deleting local dump file."
rm -f "$DUMP_PATH"
else
echo "Failed to upload $DUMP_FILE to S3. Keeping local file."
fi
else
echo "Failed to take memory dump for PID $JAVA_PID."
fi
}
# Run the function
take_memory_dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment