Automating Disk Space Alerts with Bash on Ubuntu Server
Why Full Disks Ruin Weekends
We've all been there. It's 2 AM on a Sunday. Your phone goes off. The site is down. Why? Because some random, runaway log file decided to eat the last 500MB of your server. Infuriating, right? You shouldn't have to manually babysit storage. It's a massive waste of time. Machines are built to do the boring stuff. Let them do it.
Enter Bash. Your Less-Annoying Colleague.
Forget heavy monitoring software for a second. You don't always need a bloated enterprise dashboard just to tell you your main drive is choking. A simple bash script is lighter. It's faster. And it won't cost you a dime. We just need to grab the output of a basic system command, parse it, and scream if the number gets too high. Simple. Elegant. Hard to break.
The Dirty Work: Writing the Alert Script
Time to get our hands dirty. You'll want to run `df -H` and pipe it right into `awk`. This lets you isolate the exact percentage of space used on your root partition. If it hits 90%? Boom. We trigger an email alert. I usually set my disk space alert script threshold at 85%. Gives me enough time to actually finish my coffee before I have to log in and nuke old backups. Save that logic. Make it executable. Move on.
Setting it on Autopilot with Cron
Writing the code is only half the battle. If you have to run it yourself, you haven't automated anything at all. That's where cron steps up. Open your crontab. Drop in a single line to run your new script every hour. Maybe every ten minutes if your application writes data like a maniac. I won't judge. Now your ubuntu monitoring bash setup is actually working for you quietly in the background.
Don't Trust It. Test It.
Never blindly trust a script you just wrote. Ever. Lower the threshold to 10% temporarily so it's absolutely guaranteed to trigger. Kick back and wait for the cron disk check to fire. Did your phone buzz with the email? Good. If it didn't, your local mail configuration is probably broken. Fix it. Put the threshold back to a sane number. Close the terminal.