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"
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
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]