1. Goal
Warn when a partition exceeds a threshold (e.g. 85%) before the server gets stuck due to full disk.
2. Example Bash script
#!/usr/bin/env bash
set -euo pipefail
THRESHOLD=85
LOGFILE=/var/log/disk-check.log
df -P | awk 'NR>1 {print $5, $6}' | while read -r use mountp; do
pct=${use%%%}
if [ "$pct" -ge "$THRESHOLD" ]; then
echo "$(date '+%F %T') WARN ${mountp} ${pct}%" >> "$LOGFILE"
fi
done
Save for example as /usr/local/sbin/disk-check.sh and make it executable:
chmod +x /usr/local/sbin/disk-check.sh
3. Manual test
/usr/local/sbin/disk-check.sh
tail -n 50 /var/log/disk-check.log
4. Schedule with cron
chmod +x /etc/rc.d/rc.crond
/etc/rc.d/rc.crond start
crontab -e
# Check every 15 minutes
*/15 * * * * /usr/local/sbin/disk-check.sh
5. Possible improvements
- Email with
mail/mailx - Telegram/Discord webhook
- Exclude temporary mounts
- Check inodes with
df -i
6. Useful checks
df -h
df -i
du -sh /var/log/* | sort -h | tail
Even a simple script is better than discovering the issue when a service is already down.
Quick checklist
[ ] Script created and tested
[ ] rc.crond active
[ ] Cron configured
[ ] Log checked
[ ] Threshold appropriate for the server