Alert on Low Disk Space with Cron
To get warned before a disk fills up, run a small df check from cron that only sends an alert when usage crosses a threshold. Put the logic in a script and call it hourly.
The check script
Create /usr/local/bin/disk-alert.sh:
#!/usr/bin/bash
USE=$(df / | awk 'NR==2{print $5}' | tr -d '%')
if [ "$USE" -ge 90 ]; then
echo "Disk / is ${USE}% full on $(hostname)" | mail -s "Disk alert" you@example.com
fi
chmod +x /usr/local/bin/disk-alert.sh.
The crontab line
crontab -e, then run it hourly:
0 * * * * /usr/local/bin/disk-alert.sh
Only alert on change
Alerting every hour while the disk stays full is noisy. Have the script write a state file and only email when usage first crosses the threshold, or send to a monitoring service that de-duplicates.
Send to Slack instead of email
Replace the mail line with a curl to a Slack incoming webhook if you prefer chat alerts.
FAQ
How do I alert on low disk space with cron?
Run a script hourly that reads df usage with awk and emails (or Slacks) only when usage crosses a threshold like 90%. Cron line: 0 * * * * /usr/local/bin/disk-alert.sh.
How do I get the disk usage percentage in a script?
USE=$(df / | awk 'NR==2{print $5}' | tr -d '%') gives the integer percent for /, which you can compare with -ge in bash.
How do I stop repeated disk-full alerts every hour?
Write a state file and only send when usage first crosses the threshold, or route to a monitoring service that de-duplicates alerts.