Tag Archives: cron

Email alert for low disk space

This is a little bash script that will send an email to your specified address when the disk space on a partition is bellow a certain value.

subject="whatever you wanna write"

disk_p=`/bin/df -h / | /bin/grep $partition | /usr/bin/awk '{ print $5; }' | /bin/sed -e 's/%//'`
disk_v=`/bin/df -h / | /bin/grep $partition | /usr/bin/awk '{ print $4; }'`

if [ $disk_p -gt $percent_limit ]; then
#  /bin/echo "Disk space is $disk_p% - $disk_v free! "
  /bin/echo "$subject" | /usr/bin/mail $email -s "Disk space is $disk_p% - $disk_v free! "

You just have to change the first 4 lines to your needs. Save the bash script and do a

chmod +x /path/to/script

Now we just have to make it run every X hours. For that just add the following line in the cron (crontab -e) :

# low disk warning
01  */X *  *  *  /path/to/script.sh  1>/dev/null 2>/dev/null

Exit from the crontab editor with ctrl+x and wait for the script to do it’s magic.

Hope this helped, it sure helped me.