Skip to content

Instantly share code, notes, and snippets.

@feldim2425
Last active April 18, 2020 23:26
Show Gist options
  • Save feldim2425/0c94b4e9dc56754a073f3cdfbd7688da to your computer and use it in GitHub Desktop.
Save feldim2425/0c94b4e9dc56754a073f3cdfbd7688da to your computer and use it in GitHub Desktop.
#!/bin/sh
COUNT_SWAP=0 # Set this to 1 if you want to count free swapspace as free memory
WARN=$(( 2 * 1024 * 1024 )) # 2GB has to be in KB so multiply with 1024 2-times
INTERVAL=10 # check every 10 seconds
INTERVAL_N=30 # check every 30 seconds after the first notification
AVAILABLE_MEM=$((`awk '/MemAvailable/{print $2}' /proc/meminfo`)) # get available memory
AVAILABLE_SWAP=$((`awk '/SwapFree/{print $2}' /proc/meminfo`)) # get swap memory
INTV_C=$INTERVAL
while true; do
AVAILABLE=$AVAILABLE_MEM
if [ $COUNT_SWAP -gt 0 ]; then
AVAILABLE=$(($AVAILABLE + $AVAILABLE_SWAP))
fi
if [ $AVAILABLE -lt $WARN ]; then
# Send notification
notify-send -u critical "Warning! Available RAM $(($AVAILABLE_MEM / 1024)) MB & $(($AVAILABLE_SWAP / 1024)) MB Swap"
INTV_C=$INTERVAL_N
else
INTV_C=$INTERVAL
fi
sleep $INTV_C
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment