0
0
Bash Scriptingscripting~15 mins

Disk space monitoring script in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Disk space monitoring script
📖 Scenario: You are a system administrator who wants to keep an eye on disk space usage on a server. If disk space usage goes above a certain limit, you want to be alerted so you can take action before the server runs out of space.
🎯 Goal: Build a simple bash script that checks the disk space usage of the root directory and alerts if usage is above a set threshold.
📋 What You'll Learn
Create a variable to store the disk usage percentage of the root directory
Create a variable to store the threshold percentage for alerting
Write an if statement to compare disk usage with the threshold
Print an alert message if disk usage is above the threshold
Print a safe message if disk usage is below or equal to the threshold
💡 Why This Matters
🌍 Real World
System administrators often need to monitor disk space to prevent servers from running out of storage, which can cause crashes or data loss.
💼 Career
Knowing how to write simple monitoring scripts is a key skill for DevOps engineers and system administrators to maintain healthy infrastructure.
Progress0 / 4 steps
1
Get disk usage percentage
Create a variable called disk_usage that stores the disk usage percentage of the root directory / using the df command and awk. Extract only the number without the percent sign.
Bash Scripting
Need a hint?

Use df / to get disk info for root, then use awk to get the 5th column of the second line and remove the percent sign.

2
Set alert threshold
Create a variable called threshold and set it to 80 to represent 80% disk usage as the alert limit.
Bash Scripting
Need a hint?

Just assign the number 80 to a variable named threshold.

3
Compare disk usage with threshold
Write an if statement that compares disk_usage with threshold. If disk_usage is greater than threshold, print "Disk space is above threshold!". Otherwise, print "Disk space is within safe limits.".
Bash Scripting
Need a hint?

Use [ "$disk_usage" -gt "$threshold" ] inside an if statement to compare numbers.

4
Run the script and display result
Run the script and print the alert or safe message based on the current disk usage.
Bash Scripting
Need a hint?

Just run the script as is. It will print one of the two messages depending on disk usage.