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. There are many monitoring tools which do the monitoring and 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.

Shell script to monitor disk usage

Automate Disk Usage Monitoring

In this post, I 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.

For example,

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

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


For Ubuntu/Debian

Following is the script which tests the disk usage and sends a report over to the mailid you specify in the script. The explanation for the script is given after the snippet.

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.

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.

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.

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

Note: Change the script path accordingly.

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


By default vagrant mounts the host folder to /vagrant directory of the Virtual machine where you have the vagrantfile.…

Read more

Note: In Linux, everything is treated as files. Even folders are treated as files. There will be situations where…

Read more

When you interrupt yum installation and try to reinstall the package again , you are likely to get a transaction lock.…

Read more

Internet is huge! Help us find great content


Never miss a thing! Sign up for our newsletter to stay updated.


The Best Tutorials & Tips to Speedup Your DevOps Workflow.

Created by Bibin Wilson.



Udemy Black Friday Offer Courses For $9.99 For Limited Time