Shell script to monitor disk usage

Linux Shell Script To Automate Disk Usage Monitoring

If you are running mission-critical applications on servers, it is important to monitor and notify the administrators about the disk usage.

Many open source monitoring tools do the monitoring and can be integrated with various notification services.

But let’s say you are running a single server and you don’t have a monitoring solution in place. In this case, it is better to write a script that automates the monitoring and sends reports to mail if it reaches the threshold.

Automate Disk Usage Monitoring

In this post, we have explained a shell script that monitors the disk usage at a regular interval you specify in the crontab and emails the report if it reaches the threshold you specify.

Let’s Implement It

First, you need to make sure that mail command is working on your server to get the notifications. You can do this test by executing the following command.

echo "This message will go into the body of the mail." | mail -s "Testing from the server." (email id here)

For example,

echo "This message will go into the body of the mail." | mail -s "Testing from the server." [email protected]

While executing the above command, you might get the following error.

-bash: mail: command not found

If you get the above error, install mail component using the following command and test the command again. Otherwise, proceed to next step.


sudo yum install mailx

For Ubuntu/Debian

sudo apt-get install mailutils

Script to Test Disk Usage

Following is the script that tests the disk usage and sends a report over to the mail id you specify. The explanation for the script is given after the snippet.


MAILID="[email protected]"




for line in $(df -hP | egrep '^/dev/' | awk '{ print $1 "_:_" $5 }')
    FILESYSTEM=$(echo "$line" | awk -F"_:_" '{ print $1 }')
    DISK_USAGE=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )

    if [ $DISK_USAGE -ge $VALUE ];
      EMAIL="$SERVERNAME - $(date): $FILESYSTEM Exceeded the threshold VALUE\n"
      EMAIL="$EMAIL\n Usage Details\n Current Usage:($DISK_USAGE%) Threshold value: ($VALUE%)"
      echo -e "$EMAIL" | $MAIL -s ""$SERVERNAME" Disk Usage Alert: Needs Attention!" "$MAILID"
    elif [ $DISK_USAGE -lt $VALUE ];
      EMAIL="$EMAIL\n$FILESYSTEM ($DISK_USAGE%) is lessthan the threshold ($VALUE%)"
      echo -e "$EMAIL" | $MAIL -s ""$SERVERNAME" Disk Usage Alert: Threshold Not Reached" "$MAILID"

Monitoring Script explanation

The above script gets the memory information using “df hP” command.

The environment variable MAILID holds the email id in which the report has to be sent.

VALUE holds the threshold you want to set.

SERVERNAME holds the hostname of the server for server identification in the email notification.

Using a for loop it will loop through all the filesystem which has “/dev” mount point. Then it gets the filesystem name and disk usage percentage to two variables FILESYSTEM and DISK_USAGE.

Using the above variables the disk usage is equated with the threshold VALUE variable. Based on the result, an email will be sent to the specified id.

Monitoring Script Execution

To execute the script, copy the script content on to a file named

Now, change the execution permission of the script using the following command.

chmod +x

To run the script, using the following command.


Automating The Monitoring Process

Running the script manually does not make any sense as we want to automate the process. The best way to automate the process is by adding the script to crontab entry.

You can open the crontab using the following command.

crontab -e

Add the following line to run the script every 5 minutes.

Note: Change the script path accordingly.

*/5 * * * * sh /home/ec2-user/

Now you can list the crontab entry using the following command.

crontab -l

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap