Operations 5 min read

Automate Disk Usage Monitoring with a Bash Script and Email Alerts

This guide shows how to install mail utilities on CentOS 8, write a Bash script that checks disk usage, send threshold‑exceeding reports via email, and schedule the script with cron for continuous, automated monitoring of critical servers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Automate Disk Usage Monitoring with a Bash Script and Email Alerts

If a server runs critical workloads, continuously monitoring its disk usage and notifying administrators is essential. The article walks through creating a Bash script that runs at regular intervals, evaluates each mounted filesystem, and emails a report when usage exceeds a defined threshold.

Prerequisites

Ensure the mailx command and postfix mail service are installed on CentOS 8:

yum -y install mailx postfix
systemctl enable --now postfix

Test the mail setup with:

echo "This message will go into the body of the mail." | mail -s "Testing from the server." root@localhost

Disk‑monitoring script

Create disk-monitor.sh and add the following content:

#!/bin/bash

MAILID="[email protected]"
VALUE=80
SERVERNAME=$(hostname)
MAIL=/bin/mail

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

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

Explanation of key variables: MAILID – the email address that receives the report. VALUE – the usage percentage threshold (e.g., 80%). SERVERNAME – the host name, used in the email subject. FILESYSTEM and DISK_USAGE – extracted from the df -hP output for each /dev mount point.

Running the script

Make the script executable and run it manually to verify:

chmod +x disk-monitor.sh
./disk-monitor.sh

Automating with cron

To have the script execute automatically, add a cron entry. Open the crontab editor: crontab -e Insert a line such as the following to run the script every five minutes: */5 * * * * sh /home/ec2-user/disk-monitor.sh With this setup, the server continuously monitors disk usage and sends email alerts only when the defined threshold is crossed, eliminating the need for manual checks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cronshell scriptDisk Monitoringemail alerts
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.