The Script
Save as disk-email-alert.sh (or any name). Set EMAIL and THRESHOLD, install mailutils, then run or cron it.
When the threshold trips
The script logs the percentage, fills MESSAGE with host, time, and du output, sends one email via mail -s …, and prints confirmation. Below is how each piece fits together.
How It Works
HOSTNAME=$(hostname) runs the hostname command once and stores the machine name in HOSTNAME, so subject lines and the body clearly say which server had the problem (especially useful if you run the same script on several VPS hosts).
The multi-line MESSAGE variable uses double quotes so Bash expands variables like ${USAGE} and embedded command substitution ($(du …)) when the assignment runs. Literal newlines inside the quoted string appear as real line breaks in the email body.
du -sh /* estimates size per top-level directory (human-readable sizes with -h). Piping through sort -rh and head -5 surfaces the worst offenders inside the alert.
echo "$MESSAGE" | mail -s "[ALERT] Disk Space on ${HOSTNAME}" "$EMAIL" sends standard input as the body, -s sets the subject, and the last argument is the recipient. That is the common mailutils pattern on Ubuntu when local/SMTP delivery is configured.
Important notes
You must have mailutils installed: sudo apt install mailutils
Step-by-Step Setup
Step 1 — Install mailutils
When the installer asks, choose Internet Site and use your server hostname. Test delivery: echo "test" | mail -s "test" you@example.com
Step 2 — Create the script
Paste the script, then Ctrl+X → Y → Enter to save.
Step 3 — Set your email and threshold
Edit EMAIL="you@example.com" and THRESHOLD=80 to match your inbox and risk tolerance.
Step 4 — Make it executable
Step 5 — Run it once
Schedule It with Cron
Automation is the point: let the script run while you sleep.
Open your crontab
Example schedules
� Cron errors by mail
To capture stderr from a cron line: append 2>&1 | mail -s 'Cron Error' you@example.com so failures land in your inbox.
Variations
Use Gmail SMTP via msmtp (config example)
Install msmtp, then configure a relay. Example snippet for ~/.msmtprc — use an App Password, not your normal Gmail password:
Point mail at msmtp per your distro docs, or call msmtp directly from the script.
One email for multiple conditions (disk + RAM + CPU)
Collect flags in variables, build a single BODY, and send once if anything is red:
Send at most one alert per 24 hours (lockfile)
Use a sentinel file so a flapping condition does not hammer your inbox:
Get $200 Free →
Common Mistakes
Sending from cron without a full path
Cron’s PATH is minimal. Use absolute paths for the script on the cron line and optionally for mail inside the script ($(command -v mail)).
Mail goes to root@hostname locally
If SMTP is not set up, mail may only deliver locally. Confirm with a test message and watch /var/log/mail.log (path varies).
Understanding the Commands
| Piece | Role |
|---|---|
| hostname | Prints the system hostname for subjects and body |
| date '+%Y-%m-%d %H:%M:%S' | Timestamp for logs and the email body |
| df / | awk … | tr -d '%' | Numeric root filesystem use % for comparisons |
| du -sh /* | sort -rh | head -5 | Top-level directory sizes; helps you act on the alert |
| mail -s "subject" user | Reads body from stdin; sends via configured MTA |
Frequently Asked Questions
How do I send an email from a bash script?
Use the mail command: echo 'Message body' | mail -s 'Subject' you@example.com. Install mailutils first on Ubuntu: sudo apt install mailutils
How do I install mailutils on Ubuntu?
Run: sudo apt update && sudo apt install mailutils. When prompted, choose Internet Site and use your server hostname.
Can I send email alerts automatically from a cron job?
Yes. Schedule your script with crontab -e. When the condition is met, the script sends the email. Add 2>&1 | mail -s 'Cron Error' you@example.com to capture cron output.
What if mail command is not found?
Run: which mail — if it returns nothing, install with: sudo apt install mailutils (Ubuntu/Debian) or sudo yum install mailx (CentOS/RHEL).
How do I send email from bash using SMTP (Gmail)?
Use ssmtp or msmtp with your Gmail SMTP credentials. Configure /etc/ssmtp/ssmtp.conf with your gmail address and an App Password (not your real password).
Launch your Droplet →
Find your domain →
Try the Generator →