Skip to content

Instantly share code, notes, and snippets.

@spham
Forked from fduran/gist:1870429
Created September 13, 2016 10:03
Show Gist options
  • Select an option

  • Save spham/62fe0f2fa6d882627f27a9034c7cc5aa to your computer and use it in GitHub Desktop.

Select an option

Save spham/62fe0f2fa6d882627f27a9034c7cc5aa to your computer and use it in GitHub Desktop.
Linux disk space email alert
#!/bin/bash
# www.fduran.com
# script that will send an email to EMAIL when disk use in partition PART is bigger than %MAX
# adapt these 3 parameters to your case
MAX=95
[email protected]
PART=sda1
USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL
fi
@spham
Copy link
Author

spham commented Sep 13, 2016

If you don't want to step up to a full monitoring solution such as Nagios you can create your own scripts for monitoring the things that you want to monitor, such as disk space. The following script alerts you when your root partition is almost full:

`

!/bin/bash

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' [email protected] << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
`
The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD varialbe (90% here).

To run it daily, for example, save the script to the file sample.sh in your home directory, change the email to your email, and add the following line at the end of /etc/crontab file:

@daily ~/sample.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment