Disk Alert Script
Write a Bash script that checks how full each disk on a Linux machine is and warns you before one runs out of space. It reads disk usage with df, uses awk to extract each filesystem's usage percentage, and compares it with a threshold you choose (80% by default). Anything over the limit is printed as a warning and can also be sent as a message to a webhook URL with curl, so you get alerts in a chat tool like Slack or Discord. Finally, you'll schedule it with cron so it runs automatically. You'll learn Bash basics: variables, arguments, loops, conditions and exit codes, and how to combine command-line tools with pipes.
- Estimate
- ~8h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
curl · GNU Awk
Project roadmap
- 01
Create the script
~1hCreate disk_alert.sh starting with #!/usr/bin/env bash and set -euo pipefail, and make it executable with chmod +x. Add a usage function and read two optional flags with getopts: -t for the threshold percentage (default 80) and -w for a webhook URL. Check that the threshold is a whole number between 1 and 99; if not, print the usage and exit with code 2. Done when: ./disk_alert.sh -h shows the usage and an invalid -t value is rejected.
- 02
Read disk usage with df and awk
~1.5hRun df -P (the portable format, one line per filesystem) and pipe it into awk. Skip the header line and pseudo-filesystems such as tmpfs and devtmpfs, and print each mount point with its usage as a plain number, without the % sign. Done when: this part on its own prints one line per real filesystem, such as / 42.
- 03
Compare against the threshold
~1.5hLoop over the lines from step 2 with while read. For each filesystem at or above the threshold, print a warning like WARNING: / is 91% full (threshold 80%). If nothing is over the limit, print OK: all filesystems below 80%. Exit with code 1 when at least one warning was printed and 0 otherwise, so other tools can react. Done when: -t 1 prints warnings and exits with 1, and -t 99 prints OK and exits with 0.
- 04
Send an alert with curl
~1.5hWhen -w is given and there are warnings, send one message containing all of them to the webhook with curl -sS --fail -X POST and a JSON body such as {"text": "..."}. If curl fails, print an error to stderr but still exit with the normal warning code. Done when: using a test webhook (for example from webhook.site), a run over the threshold delivers the message.
- 05
Schedule it with cron
~1hAdd a section to the README that explains how to run the script every 15 minutes with cron, with an example crontab line using the full path to the script and sending its output to a log file. Try the crontab line on your own machine. Done when: the README contains the cron instructions and the example line.
- 06
Check with ShellCheck and test edge cases
~1.5hRun shellcheck disk_alert.sh and fix every warning. Then test these cases and list them in the README with the result: a threshold of 0 or 100 (rejected), no webhook (warnings are only printed), and a webhook URL that doesn't exist (an error is printed and the script still finishes). Done when: shellcheck reports no issues and the README lists the tested cases.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack