Skip to content

Instantly share code, notes, and snippets.

@nickrussler
Last active October 6, 2020 13:28
Show Gist options
  • Save nickrussler/0e306945f13caf35e7c9cf08d0a1cc2a to your computer and use it in GitHub Desktop.
Save nickrussler/0e306945f13caf35e7c9cf08d0a1cc2a to your computer and use it in GitHub Desktop.
Wait with bash for some amount of memory (MemAvailable) to be available
#!/bin/bash
# Wait for MemAvailable to be bigger than a certain value.
#
# $1 - Value in kb that MemAvailable should be greater than.
# $2 - (Optional, Default=60) Timeout in seconds
#
# Examples
#
# wait_for_mem_available 10000000 120
wait_for_mem_available() {
REQUESTED_MEMORY_KB=$1
TIMEOUT_DURATION=$((${2:-60}))
TIMEOUT_END=$((SECONDS+TIMEOUT_DURATION))
while true; do
mem_available=$(grep MemAvailable /proc/meminfo | awk '{ print $2 }')
if [ $SECONDS -gt $TIMEOUT_END ]; then
echo "Stopped waiting for $REQUESTED_MEMORY_KB kb MemAvailable after $SECONDS seconds (MemAvailable=$mem_available kb)"
break
fi
if [ "$mem_available" -gt "$REQUESTED_MEMORY_KB" ]; then
break
fi
sleep 1
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment