How to monitor disk space usage with shell script

It is an important task for Linux administrators to monitor disk space usage on any system that’s used for hosting critical applications, in order to prevents the system from becoming unresponsive or get into an unknown problem.

There are various monitoring tools available that monitor everything, and trigger an email when the system reaches a given threshold.

However, it is suitable for small and large environments, but what is the best approach if you only have a few systems?

In this case, it is better to write a shell script based on what you need to monitor in your environment. We have included three shell scripts below and you can choose the one that suits your need.

These scripts will trigger an email to the corresponding email id when the system reaches a given threshold.

Refer to this article (embedded link), If you are looking for a bash script to monitor disk space usage on multiple remote Linux systems.

Make a note: When using “df -h” in the shell script in some distributions, you may end up with the below error message as the output is not in the correct format. Use “df -Ph” (POSIX output format) to deal with this problem.

# sh /opt/script/disk-usage-alert-old.sh

/dev/mapper/vg_2g-lv_root
test-script.sh: line 7: [: /dev/mapper/vg_2g-lv_root: integer expression expected
/ 9.8G

Method-1: Shell script to monitor disk space usage in Linux

This script is very simple and straightforward, which triggers an email when the system reaches a given threshold.

In this example, we set threshold at 60% for testing purpose and you can change this limit based on your requirements. Be sure to include your email ID in the script.

# vi /opt/script/disk-usage-alert.sh

#!/bin/sh
dusage=$(df -Ph | grep -vE '^tmpfs|cdrom' | sed s/%//g | awk '{ if($5 > 60) print $0;}')
fscount=$(echo "$dusage" | wc -l)
if [ $fscount -ge 2 ]; then
echo "$dusage" | mail -s "Disk Space Alert On $(hostname) at $(date)" [email protected]
else
echo "Disk usage is in under threshold"
  fi

Set an executable permission to the file “disk-usage-alert.sh” :

# chmod +x /opt/script/disk-usage-alert.sh

Run the following script to see if it works as expected:

# sh /opt/script/disk-usage-alert.sh

Sample email alert: You will receive an email alert similar to the one below.

Filesystem                            Size  Used Avail Use Mounted on
/dev/mapper/vg_2g-lv_root 10G 6.7G 3.4G 67 /
/dev/mapper/vg_2g-lv_home 5.0G 4.3G 784M 85 /home

Finally add a cronjob to automate this as shown below. It will run every 10 minutes.

# crontab -e
*/10 * * * * /bin/bash /opt/script/disk-usage-alert.sh

Note: As the script is scheduled to run once every 10 minutes, you will receive an email alert every 10 minutes.

For example: if your system reaches the given limit after 16 minutes, you will receive an email alert in the second cycle, i.e. after 20 minutes (2nd 10 minute cycle).

Method-2: Monitoring disk space usage with shell script

Alternatively, the following shell script can be used to monitor disk space usage on Linux systems.

This script send multiple emails if more than one file system reaches the given threshold limit, as the script is uses a loop (notice the do logic below):

# vi /opt/script/disk-usage-alert-1.sh

#!/bin/sh
df -Ph | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5,$1 }' | while read output;
do
  echo $output
  used=$(echo $output | awk '{print $1}' | sed s/%//g)
  partition=$(echo $output | awk '{print $2}')
  if [ $used -ge 60 ]; then
  echo "The partition \"$partition\" on $(hostname) has used $used% at $(date)" | mail -s "Disk Space Alert: $used% Used On $(hostname)" [email protected]
else
echo "Disk space usage is in under threshold"
  fi
done

Output: You will get several email alerts if more than one file system reaches the given threshold, as shown below:

The partition "/dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Wed Feb 24 06:16:14 IST 2021

The partition "/dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Wed Feb 24 06:16:14 IST 2021

The best way to automate the process is to add the script to crontab, which will trigger an email alert whenever it reaches a given threshold.

# crontab -e
*/10 * * * * /bin/bash /opt/script/disk-usage-alert-1.sh

Method-3: Monitoring Disk space usage for a specific partition

If you want to monitor a particular partition then you can use the following shell script. Simply replace your file system name instead of ours in the script:

# vi /opt/script/disk-usage-alert-2.sh

#!/bin/bash
used=$(df -Ph | grep '/dev/mapper/vg_2g-lv_dbs' | awk {'print $5'})
max=80%
if [ ${used%?} -ge ${max%?} ]; then
echo "The Mount Point "/DB" on $(hostname) has used $used at $(date)" | mail -s "Disk space alert on $(hostname): $used used" [email protected]
fi

Sample email alert:

The partition /dev/mapper/vg_2g-lv_dbs on 2g.CentOS6 has used 82% at Wed Feb 24 06:16:14 IST 2021

Add a cronjob to automate this.

# crontab -e
*/10 * * * * /bin/bash /opt/script/disk-usage-alert-2.sh

Conclusion

These shell scripts can help you to monitor and prevent your system from becoming unresponsive due to disk space issues.

Feel free to leave a comment if you have any questions.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

15 Comments on “How to monitor disk space usage with shell script”

  1. Great script. The output does not have a ‘%’ nesxt to the 79 as seen below…

    Filesystem Size Used Avail Use Mounted on
    /dev/sda2 497M 390M 108M 79 /boot

    is is possible to add that to the output? like seen in the df command…
    /dev/sda2 497M 390M 108M 79% /boot

    Thanks!

  2. i could not see the solutions, looks like that solution removed from the list.

    Could you please provide the right command, i am still getting blank emails when threshold below 60%

  3. /10 * * * * df -Ph | sed s/%//g | awk ‘{ if($5 > 60) print $0;}’ | mail -s “Disk Space Alert On $(hostname)” [email protected] iam getting a mail with only heading even if space not exceeded any way to avoid this

    1. I had the same problem. My solution is:

      /10 * * * * df -Ph | sed -n ‘1!p’ | sed s/\%//g | awk ‘{ if($5 > 30) print $0;}’ | mail -s “Disk Space Alert On $(hostname)” [email protected]

      As you can see i have added a quoting before % in my cron file.

          1. Can some one help me out with my below concern?
            Issue : need to setup one automatic job to perform disc space clear on Linux machine but the job which we create should be outside the Linux machine , so once we trigger that job the space in that Linux machine needs to be get cleared
            Any suggestions?

      1. Sorry, where is the fixed command, i saw its removed from the post.

        Could you please let me know the correct command.

Leave a Reply

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