What if your server suddenly stops because the disk is full--and you didn't even know it was close?
Why Disk space monitoring script in Bash Scripting? - Purpose & Use Cases
Imagine you manage a server that stores important files. Every day, you have to check if the disk is almost full by logging in and typing commands manually.
This manual checking is slow and easy to forget. If the disk fills up, your server might stop working, causing stress and lost data.
A disk space monitoring script runs automatically and alerts you when space is low. It saves time and prevents surprises by watching the disk for you.
df -h
# Then read output and decide if space is lowif [ $(df / | tail -1 | awk '{print $5}' | sed 's/%//') -gt 80 ]; then echo 'Disk space low!'; fi
This script lets you catch disk space problems early, keeping your server healthy without constant manual checks.
A website admin uses a disk space script to get an email alert before the server runs out of space, avoiding downtime during busy hours.
Manual disk checks are slow and easy to miss.
Scripts automate monitoring and alert you early.
Early warnings keep servers running smoothly.
Practice
Solution
Step 1: Understand the script's goal
The script's goal is to monitor disk usage to prevent running out of space.Step 2: Identify the correct purpose
Among the options, only checking disk usage and alerting matches the monitoring function.Final Answer:
To check disk usage and alert when space is low -> Option AQuick Check:
Disk monitoring = alert on low space [OK]
- Confusing monitoring with automatic deletion
- Thinking it speeds up disk operations
- Assuming it performs backups
df and awk in bash?Solution
Step 1: Understand
The command shows disk usage for root; the second line (NR==2) has the data.df -h /outputStep 2: Use
This prints the 5th column (usage %) from the second line, which is correct.awk 'NR==2 {print $5}'to get usage percentFinal Answer:
df -h / | awk 'NR==2 {print $5}' -> Option DQuick Check:
Second line, fifth column = usage percent [OK]
- Printing all lines instead of second line
- Using NR==1 which is header line
- Using sed incorrectly to get the line
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$usage" -gt 80 ]; then
echo "Warning: Disk usage is above 80%"
else
echo "Disk usage is normal"
fiWhat will be the output if the disk usage is 75%?
Solution
Step 1: Extract usage value
The script removes % sign and stores 75 in usage variable.Step 2: Compare usage with 80
75 is not greater than 80, so the else branch runs.Final Answer:
Disk usage is normal -> Option AQuick Check:
75 <= 80 means normal usage [OK]
- Forgetting to remove % sign before comparison
- Using string comparison instead of numeric
- Misreading the if condition
usage=$(df / | awk 'NR==2 {print $5}')
if [ $usage -gt 90 ]; then
echo "Disk almost full"
fiWhat problem will occur when running this script?
Solution
Step 1: Check variable usage in test
The variable $usage contains a value like '85%'. Without quotes, test may fail if value has spaces or special chars.Step 2: Identify missing % removal and quotes
Also, % sign is not removed, causing numeric comparison error. Missing quotes cause syntax error.Final Answer:
Syntax error due to missing quotes around $usage -> Option BQuick Check:
Always quote variables in test to avoid syntax errors [OK]
- Not quoting variables in if test
- Not removing % sign before numeric test
- Assuming df or awk commands fail
Solution
Step 1: Understand requirement to check all partitions
We must check each mounted partition's usage, not just total or root.Step 2: Loop through
Extract usage percent per line, compare with 85, and alert if exceeded.df -houtput lines and compare usageFinal Answer:
Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded -> Option CQuick Check:
Loop and compare each partition usage [OK]
- Checking only total or root partition
- Using du instead of df for disk usage
- Not looping through all partitions
Ready to prove it?
Pass the test and this pattern moves unknown
