Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active October 2, 2020 13:49
Show Gist options
  • Save bergantine/1163864 to your computer and use it in GitHub Desktop.
Save bergantine/1163864 to your computer and use it in GitHub Desktop.
Send an email when memory hits a certain limit (Bash CRON script for Linux). #bash #cron #linux #ops
# check memory usage every 15 minutes and if usage matches 90% of allocation, send an email
# where _username_ is the username (works on Mac if -u parameter is changed to -U)
# where [email protected]_ is who should be emailed
# where MAX is the allocated memory (must be an integer)
# where LIMIT is the point at which you want to be emailed (must be an integer)
*/15 * * * * USAGEF=`ps -u _username_ -o rss,command | grep -v peruser | awk '{sum+=$1} END {print sum/1024}'`; USAGEI=${USAGEF/.*}; MAX=120; LIMIT=108; if [ $USAGEI -gt $LIMIT ]; then echo Usage \($USAGEI\) is approaching or over the maximum allocation \($MAX\) | mail -s "Server is approaching max allocation of memory." [email protected]_; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment