I’d like my linux box to email me when it’s hard drive is full.

I’d like my linux box to email me when it’s hard drive is full.

It could be my webserver, my home file server full of movies and music, or even my netbook after getting a bit hit of Dropbox files. It doesn’t matter, I’d just like an easy way of getting told that one of the drives on one of my boxes is getting full. If I’ve filled it too far or downloaded too much, I’d like it to email me.

Well, the code below does just that. All you need to do is set the email details for where you wish to receive the alert, and at which percentage of use – eg ALERT = 60 means to send alerts when over 60% of the partition is full.

#!/bin/bash
# set email address for recipient
EMAILADDY="[email protected]"
# set alert level in percent
ALERT=60
df -HP | grep -vE '^Filesystem|tmpfs|cdrom' |
while read partition size used free perc mnt ;
do
    usep=$(echo $perc | tr -d '%' )
    if [ $usep -ge $ALERT ]; then
    echo "Low space: \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space - $usep%" $EMAILADDY
fi
done

Simply edit as required and save the above as an executable shell script in /etc/cron.daily/ or /etc/cron.hourly/ and you’re good to go. You probably want to change the notification email address or else I’ll be the only one who knows when your drives are filling up.

The automated emailing (and otherwise sending email from the command line in linux) does require the “mail” command which is part of “sendmail” package on Debian/Ubuntu and may not be installed by default on some desktop/barebones systems.

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in linux Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*