Created
October 1, 2024 19:03
-
-
Save jabez007/b62b1e11274aaa000cd705bb6a6ae98a to your computer and use it in GitHub Desktop.
bash script for tracking Docker container memory usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if container ID is provided | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <container_id_or_name> [sleep_interval_in_seconds]" | |
exit 1 | |
fi | |
# Container ID or name from the first argument | |
CONTAINER_ID=$1 | |
# Sleep interval from the second argument, defaulting to 5 seconds if not provided | |
SLEEP_INTERVAL=${2:-5} | |
# File to store peak memory usage | |
PEAK_MEMORY=0 | |
# Function to clear the current line | |
clear_line() { | |
echo -ne "\r\033[K" | |
} | |
while true; do | |
# Get the current memory usage from docker stats | |
CURRENT_MEMORY=$(docker stats --no-stream --format "{{.MemUsage}}" $CONTAINER_ID | awk -F'/' '{print $1}' | awk '{print $1}' | sed 's/MiB//') | |
# Check if the current memory usage is greater than the peak | |
if (( $(echo "$CURRENT_MEMORY > $PEAK_MEMORY" | bc -l) )); then | |
PEAK_MEMORY=$CURRENT_MEMORY | |
fi | |
# Clear the line before printing the new values | |
clear_line | |
echo -ne "Current Memory: ${CURRENT_MEMORY}MiB, Peak Memory: ${PEAK_MEMORY}MiB" | |
# Sleep for the specified interval before checking again | |
sleep "$SLEEP_INTERVAL" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment