Skip to content

Instantly share code, notes, and snippets.

@yuwash
Last active May 27, 2018 13:53
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 yuwash/6648f953a5c8e3f1dd83c453ed2f5ebc to your computer and use it in GitHub Desktop.
Save yuwash/6648f953a5c8e3f1dd83c453ed2f5ebc to your computer and use it in GitHub Desktop.
Warn on nearly full memory
#!/bin/bash
# see https://askubuntu.com/questions/276234/need-application-script-alerting-when-system-memory-is-running-out/346779#346779
# add crontab -e the following:
# * * * * * DISPLAY=:0.0 /path/to/memwarn.bash
#Minimum available memory limit, MB
if [[ -n "$1" ]]; then
THRESHOLD="$1"
else
THRESHOLD=400
fi
free_output="$(free -m)"
free_free () {
awk '/^Mem:/{print $4}'
}
free_buffers_cache () {
awk '/^Mem:/{print $6}'
}
free_available () {
awk '/^Mem:/{print $7}'
}
free_="$(free_free <<< "$free_output")"
buffcache="$(free_buffers_cache <<< "$free_output")"
available="$((free_ + buffcache))"
message="$free_output"
if [[ $available -lt $THRESHOLD ]]; then
notify-send "Memory is running out!" "$message"
paplay /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment