CS Coding with a smile

Linux bash script – mail when threshold increase of disk

Hi there!
Today at work I needed to use a script to send me a mail notification when the the disk was about to get full during an installation.

One could argue that I should’ve used some sort of bigger system like Nagios to get these messages, but in this scenario, this script that I wrote suits me better.

The script looks like this:

CURRENT=$(df / | grep / | awk ‘{ print $5}’ | sed ‘s/%//g’)

if [ “$CURRENT” -gt “$THRESHOLD” ] ; then
mail -s ‘Disk Space Alert’ carl.skantz@carlskantz.se << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%

Where the “Current” variable looks at the disk and where “Threshold” is the percentage of disk being used.

For example in this script, when the disk is (-gt ‘greater than’) over the defined threshold it should use the command “mail” to send me a message of the current state.

Configure it the way you want it and change the email to your own.
Save it as whatever.sh then add it to crontab.

Easy huh? yeah.