How to Monitor Disk Space and Set Up Email Alerts on Linux


Introduction
Running out of disk space can lead to system crashes, application failures, and data loss. To prevent this, we can automate disk monitoring using a Bash script, Postfix (with Gmail SMTP), and Cron.
This guide will walk you through:
A pre-written Bash script to check disk usage (unchanged).
Configuring Postfix to send emails via Gmail SMTP.
Setting up a Cron job for automated monitoring.
Step 1: Install Required Packages
Ensure postfix
(mail server) and mailutils
(email client) are installed:
sudo apt update
sudo apt install postfix mailutils -y
- During installation, select "Internet Site" and enter your system’s hostname.
Step 2: Configure Postfix for Gmail SMTP
Edit Postfix’s main configuration file:
sudo nano /etc/postfix/main.cf
Add/modify these lines:
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Set Up Gmail Credentials
Create a file for storing Gmail credentials:
sudo nano /etc/postfix/sasl_passwd
Add (replace with your Gmail and App Password):
[smtp.gmail.com]:587 your@gmail.com:your-app-password
⚠️ Important:
Use a Gmail App Password (not your main password).
Enable "Less secure apps" or 2FA + App Password in Gmail settings.
Generate the password hash and secure permissions:
sudo postmap /etc/postfix/sasl_passwd
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
Restart Postfix:
sudo systemctl restart postfix
Test Email Sending
Check if emails work:
echo "Test email from Postfix" | mail -s "Postfix Test" recipient@example.com
If the email doesn’t arrive, check logs:
tail -f /var/log/mail.log
Step 3: Use the Provided Bash Script
The script monitors /dev/sda6
and sends an email if usage exceeds 6%.
Save the Script
Create fs_status.sh
:
#!/bin/bash
# Monitoring the free fs space disk
FU=$(df -h | egrep -v "Filesystem|tmpfs" | grep sda6 | awk '{print $5}' | tr -d %)
TO="gangulyriju123@gmail.com"
if [[ $FU -ge 6 ]]
then
echo "Warning, disk space is low - $FU %" | mail -s "Disk SPACE ALERT!" $TO
else
echo "All good"
fi
Make It Executable
chmod +x fs_status.sh
Test the Script
Run manually:
./fs_status.sh
- If disk usage is ≥6%, an email will be sent.
Step 4: Schedule with Cron for Automatic Monitoring
To run the script every hour, add a Cron job:
crontab -e
Add this line (adjust the path to your script):
0 * * * * /path/to/disk_alert.sh >> /var/log/fs_status.log 2>&1
Verify Cron Execution
Check logs:
tail -f /var/log/fs_status.log
Check email alerts when the threshold is crossed.
Troubleshooting
If Emails Are Not Sending
Check Postfix logs:
tail -f /var/log/mail.log
Verify Gmail settings:
Enable "Less secure apps" or use an App Password.
Check Spam/Junk folder.
Test email manually:
echo "Test" | mail -s "Test" your@gmail.com
Conclusion
Setting up disk space monitoring with email alerts is a simple yet powerful way to proactively manage your Linux systems. By combining bash scripting, Postfix, and cron, you've created an automated monitoring solution that can save you from unexpected downtime.
Remember to adjust the threshold and monitoring frequency based on your specific needs, and consider expanding the script to monitor other system resources as well.
Happy monitoring!
Subscribe to my newsletter
Read articles from Sdeep directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sdeep
Sdeep
👋 Hello! I'm passionate about DevOps and I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!