Interview › Scripting (Bash, Groovy)
How would you write a script to monitor disk usage and alert past a threshold? [Advanced]
Answer
I would collect filesystem usage with df, compare the used percentage to a threshold, and alert through email, Slack, PagerDuty, or a monitoring push endpoint. The script should exclude irrelevant filesystems and exit nonzero if a threshold is breached.
Technical explanation
Use df -P for portable single-line POSIX output and parse the percentage carefully.
Avoid alerting on tmpfs, container overlay, or read-only pseudo-filesystems unless they matter.
In mature environments this should feed Prometheus/node exporter, but a shell script is useful for small systems or emergency checks.
Hands-on example
#!/usr/bin/env bash
set -euo pipefail
threshold=${1:-85}
failed=0
while read -r used mount; do
pct=${used%%%}
if (( pct >= threshold )); then
echo "ALERT: $mount is ${pct}% full" >&2
failed=1
fi
done < <(df -P -x tmpfs -x devtmpfs | awk 'NR>1 {print $5, $6}')
exit "$failed"Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- How would you write a health-check script that polls a URL until it returns 200? [Advanced]
- What is the difference between sed -i and sed without -i? [Advanced]
- What is the difference between a cron job and a systemd timer? [Advanced]
- How do you use awk to sum or average a column? [Advanced]
- What is the difference between NR and NF in awk? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- How do you split a string on a delimiter in Bash? [Intermediate]