How to setup Linux disc alert

How Can We Help?

You are here:
< Back

Log into your console at create a new file (ie. diskAlert.sh).

Make it executable: chmod +x diskAlert.sh

Now edit it and enter the following code:

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 85 ]; then
    temp_file=$(mktemp)
    echo "To: youemail@domain.com
Subject: Alert: Almost out of disk space $usep%
Content-Type: text/plain
X-Priority: 1 (Highest)
X-MSMail-Priority: High

Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" > $temp_file
cat $temp_file
/usr/sbin/sendmail -t < $temp_file
    rm ${temp_file}
	if [ $usep -ge 90 ]; then
		./sendSMS.sh #phonenumber# "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
	fi
  fi
done

The above code will send an email alert when it is above 85% used, and an SMS when above 90%. Just skip the SMS part if you don’t need it or don’t have one.

To schedule this, edit Cron using: crontab -e

0 * * * * /home/user/diskAlert > /dev/null

Using the above will check every hour.