0
0
Linux CLIscripting~30 mins

Why disk management prevents outages in Linux CLI - See It in Action

Choose your learning style9 modes available
Why Disk Management Prevents Outages
📖 Scenario: You are a system administrator responsible for keeping a Linux server running smoothly. One important task is to monitor disk usage to prevent the server from running out of space, which can cause outages and stop important services.
🎯 Goal: Build a simple script that checks disk usage on the root partition and alerts if the usage is above a certain threshold. This helps prevent outages by giving early warnings to free up space.
📋 What You'll Learn
Use the df command to get disk usage of the root partition
Create a variable to hold the disk usage percentage as a number
Set a threshold variable for maximum allowed disk usage
Use an if statement to compare usage with the threshold
Print a warning message if usage is above the threshold, otherwise print a safe message
💡 Why This Matters
🌍 Real World
System administrators monitor disk space to avoid server crashes and service interruptions caused by full disks.
💼 Career
Knowing how to automate disk usage checks and alerts is a key skill for Linux system administrators and DevOps engineers.
Progress0 / 4 steps
1
Get disk usage of root partition
Use the df command with -h and / to get disk usage of the root partition. Then create a variable called disk_usage that stores the usage percentage as a string (e.g., '75%'). Use df -h / | tail -1 | awk '{print $5}' to get this value.
Linux CLI
Need a hint?

Use df -h / to get disk info for root, then extract the fifth column for usage.

2
Set a threshold for disk usage
Create a variable called threshold and set it to 80. This means if disk usage is above 80%, we want to get a warning.
Linux CLI
Need a hint?

Just assign the number 80 to the variable threshold.

3
Compare disk usage with threshold
Remove the percent sign from disk_usage and convert it to a number called usage_num. Then use an if statement to check if usage_num is greater than threshold. If yes, set a variable alert_msg to "Warning: Disk usage is above threshold!". Otherwise, set alert_msg to "Disk usage is safe.".
Linux CLI
Need a hint?

Use ${disk_usage%?} to remove the percent sign. Use if [ "$usage_num" -gt "$threshold" ] for comparison.

4
Print the alert message
Print the value of alert_msg to show the disk usage status.
Linux CLI
Need a hint?

Use printf "%s\n" "$alert_msg" or echo "$alert_msg" to print.