Build a Bash Script to Monitor Disk Usage and Send Alerts
System administrators and DevOps engineers often need an efficient way to monitor disk usage across servers. Manual checking is not only time-consuming but also impractical at scale. In this blog post, we’ll walk through how to build a Bash script that monitors disk usage and sends an alert email if disk consumption crosses a defined threshold.
1. Understanding the Requirement
The goal is simple: we want to track disk usage with a script and notify someone via email when usage exceeds a safe percentage, such as 85%. This proactive monitoring can prevent outages caused by full disks.
We’ll break down our requirements:
- Run the script periodically using cron (optional).
- Check disk usage of the root filesystem.
- Compare usage against a threshold.
- Send an alert email if exceeded.
2. Extracting Disk Usage with Bash
The df command displays disk space usage. We’ll use it with awk to extract relevant details:
# Extract usage percentage of root file system
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
echo "Disk usage: $USAGE%"
Explanation:
df /checks the root filesystem.awk 'NR==2 {print $5}'grabs the fifth column (%Used) from the second line.tr -d '%'removes the percent sign for numeric comparison.
This isolates the disk usage in a clean, script-friendly format.
3. Adding Threshold Comparison and Notification Logic
Now we add logic to compare usage against a threshold, and if exceeded, send an alert email:
#!/bin/bash
# Configuration
THRESHOLD=85
EMAIL="admin@example.com"
# Get disk usage of root partition
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
# Check if usage is greater than threshold
if [ "$USAGE" -ge "$THRESHOLD" ]; then
SUBJECT="Disk Space Alert: root at ${USAGE}%"
MESSAGE="Warning: Your root partition '/' is ${USAGE}% full. Consider cleaning up disk space."
echo "$MESSAGE" | mail -s "$SUBJECT" "$EMAIL"
fi
The script checks whether disk usage exceeds the threshold. If so, it sends a simple email using the built-in mail command.
Note: Ensure the mail utility is installed and configured on your system (e.g., via mailutils on Debian/Ubuntu or mailx on RedHat-based systems).
4. Automating with Cron
To automate this script, schedule it with cron to run at regular intervals:
crontab -e
Add the following line to run the check every hour:
0 * * * * /path/to/disk_monitor.sh
This sets up automatic monitoring without manual intervention.
Pro Tip: Make sure the script is executable: chmod +x /path/to/disk_monitor.sh.
5. Enhancements and Best Practices
While the base script is functional, here are some ideas to enhance it for production environments:
- Monitor all mounts: Loop through all
dfentries and alert on any partition above threshold. - Log alerts: Append alerts to a log file for auditing.
- Prevent alert flooding: Use a lock file or state tracking to prevent redundant alerts.
- Disk Usage by Directory: Enhance the script to analyze large directories using
du -sh *for cleanup guidance.
Here’s how to monitor all filesystems instead of just root:
df -hP | awk 'NR>1 {print $5 " " $6}' | while read output; do
usep=$(echo $output | awk '{ print $1}' | tr -d '%')
partition=$(echo $output | awk '{ print $2}')
if [ "$usep" -ge "$THRESHOLD" ]; then
echo "Running out of space "$partition" ($usep%) on $(hostname) as on $(date)" | \
mail -s "Disk Alert: $partition usage at $usep%" $EMAIL
fi
done
This version iterates through all partitions and emails if any cross the defined usage level.
Conclusion
Proactive disk monitoring is vital for server uptime, and Bash offers a lightweight, easy-to-deploy solution. This script can be extended easily, and automating it with cron ensures consistent oversight with minimal effort. Add logging, alert dampening, and directory usage traces to evolve it into a full monitoring utility.
Stay ahead of unexpected storage issues—let Bash alert you before things break.
Useful links:


