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

← All Scripting (Bash, Groovy) questions