Skip to content

Instantly share code, notes, and snippets.

@tobek
Created December 9, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobek/7af5a74638e3e998edaa432ca50d03e8 to your computer and use it in GitHub Desktop.
Save tobek/7af5a74638e3e998edaa432ca50d03e8 to your computer and use it in GitHub Desktop.
Linux daemon for alerting you if you're running out of memory
#!/bin/bash
# Minimum available memory limit, MB
THRESHOLD=1024
# Check time interval, sec
INTERVAL=60
while :
do
# For a more detailed message:
# free=$(free -h | awk '/^Mem:/{print $4}')
# shared=$(free -h | awk '/^Mem:/{print $5}')
# bufcache=$(free -h | awk '/^Mem:/{print $6}')
# available=$(free -h | awk '/^Mem:/{print $7}')
# message="Free: $free\nShared: $shared\nBuffers/cached: $bufcache\nAvailable: $available\n\n~--\n\n$fortune"
# MAKE SURE TO CHECK THIS AGAINST YOUR DISTRO'S VERSION OF `free`! Output formats may differ and you might not want `7`.
available_mb=$(free -m | awk '/^Mem:/{print $7}')
fortune=`fortune -a` # let's make it a little fun
message="Only ${available_mb}MB available!\n·\n$fortune"
if [ $available_mb -lt $THRESHOLD ]
then
notify-send -i face-surprise -u critical -t 20000 "Wait, holy shit, you're running out of memory" "$message"
fi
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment