0
0
Bash Scriptingscripting~15 mins

Disk space monitoring script in Bash Scripting - Deep Dive

Choose your learning style9 modes available
Overview - Disk space monitoring script
What is it?
A disk space monitoring script is a small program written in bash that checks how much storage space is used and available on a computer's disks. It helps track disk usage and alerts when space is running low. This script runs commands to gather disk information and can notify users or administrators automatically. It is useful for preventing problems caused by full disks.
Why it matters
Without disk space monitoring, disks can fill up unnoticed, causing programs to crash or data loss. This can lead to downtime and lost work, which is costly and frustrating. A monitoring script helps catch these issues early by giving warnings before space runs out. It keeps systems healthy and running smoothly.
Where it fits
Before learning this, you should know basic bash commands and how to use the terminal. After this, you can learn about automating scripts with cron jobs and integrating alerts with email or messaging tools. This topic fits into system administration and DevOps automation.
Mental Model
Core Idea
A disk space monitoring script regularly checks disk usage and warns when space is low to prevent system problems.
Think of it like...
It's like a fuel gauge in a car that tells you when the gas tank is almost empty so you can refill before running out.
┌─────────────────────────────┐
│ Disk Space Monitoring Script │
├──────────────┬──────────────┤
│ Check Disk   │ Run 'df'     │
│ Usage        │ command      │
├──────────────┼──────────────┤
│ Compare      │ Usage vs     │
│ Threshold    │ limit        │
├──────────────┼──────────────┤
│ Alert User   │ Send message │
└──────────────┴──────────────┘
Build-Up - 6 Steps
1
FoundationUnderstanding Disk Usage Basics
🤔
Concept: Learn what disk space is and how to check it using simple commands.
The 'df' command shows disk space usage on your system. Running 'df -h' displays sizes in human-readable form (like GB). It lists each disk, total size, used space, available space, and mount point. This helps you see how full your disks are.
Result
You can see disk usage details for all mounted disks in a clear format.
Knowing how to read disk usage is the first step to monitoring and managing storage effectively.
2
FoundationWriting Basic Bash Scripts
🤔
Concept: Learn how to write and run simple bash scripts to automate commands.
Create a file named 'check_disk.sh' with the line '#!/bin/bash' at the top. Add commands like 'df -h' inside. Make it executable with 'chmod +x check_disk.sh'. Run it with './check_disk.sh'. This automates running commands.
Result
You can run your script to see disk usage without typing commands manually.
Automating commands with scripts saves time and reduces errors in repetitive tasks.
3
IntermediateExtracting Disk Usage Percentage
🤔Before reading on: do you think you can get just the percentage of disk used from 'df' output using bash commands? Commit to yes or no.
Concept: Learn to parse command output to get only the disk usage percentage for scripting decisions.
Use 'df -h /' to check root disk usage. Pipe output to 'awk' to extract the Use% column: df -h / | awk 'NR==2 {print $5}' This prints something like '45%'. You can remove the '%' sign with bash string manipulation.
Result
You get a clean number representing disk usage percentage, e.g., '45'.
Extracting exact values from command output allows scripts to make decisions based on disk usage.
4
IntermediateAdding Threshold Checks and Alerts
🤔Before reading on: do you think a script can send an alert automatically when disk usage passes a limit? Commit to yes or no.
Concept: Learn to compare disk usage against a limit and notify when space is low.
Set a threshold, e.g., 80%. Use an if statement: if [ "$usage" -gt 80 ]; then echo "Warning: Disk space low"; fi Replace echo with commands to send email or messages. This triggers alerts when usage is high.
Result
The script warns you when disk usage exceeds the threshold.
Automated alerts help catch disk space problems early without manual checks.
5
AdvancedScheduling Script with Cron Jobs
🤔Before reading on: do you think the script can run automatically at set times without manual start? Commit to yes or no.
Concept: Learn to automate running the script regularly using cron.
Edit crontab with 'crontab -e'. Add a line like '0 * * * * /path/to/check_disk.sh' to run every hour. Cron runs scripts in background, so you get regular checks and alerts without manual work.
Result
Disk space is monitored automatically on schedule.
Scheduling scripts ensures continuous monitoring and timely alerts without human intervention.
6
ExpertHandling Multiple Disks and Logging
🤔Before reading on: do you think monitoring multiple disks requires separate scripts or can one script handle all? Commit to your answer.
Concept: Learn to extend the script to check all disks and keep logs for history.
Use 'df -h' without arguments to list all disks. Loop over each line except header, extract usage, compare to threshold, and alert if needed. Append results and alerts to a log file with timestamps. This helps track disk usage trends over time.
Result
A single script monitors all disks and keeps a history of usage and alerts.
Comprehensive monitoring and logging provide better system insight and help diagnose long-term storage issues.
Under the Hood
The script runs the 'df' command to get disk usage data. It parses the output text line by line using tools like 'awk' or bash string operations. It compares numeric values to thresholds using conditional statements. When conditions meet, it triggers alert commands like 'echo' or 'mail'. Cron daemon schedules the script to run automatically at set intervals. Logs are simple text files where the script appends timestamped entries.
Why designed this way?
Unix-like systems provide 'df' as a standard tool to report disk usage. Parsing text output is simple and portable across systems. Bash scripting is widely available and easy to use for automation. Cron is a built-in scheduler that requires no extra setup. This design uses existing tools to build flexible, lightweight monitoring without complex dependencies.
┌───────────────┐
│ Cron Scheduler│
└──────┬────────┘
       │ Runs script at intervals
┌──────▼────────┐
│ Disk Monitor  │
│ Bash Script   │
│ - Runs 'df'   │
│ - Parses data │
│ - Checks limit│
│ - Sends alert │
└──────┬────────┘
       │ Logs alerts
┌──────▼────────┐
│ Log File      │
└───────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Does 'df' show real-time disk usage including open but deleted files? Commit yes or no.
Common Belief:People often believe 'df' always shows the exact current disk usage including all files.
Tap to reveal reality
Reality:'df' shows disk usage based on filesystem metadata, but open deleted files still consume space until closed, which 'df' may not reflect immediately.
Why it matters:This can cause confusion when disk space seems full but 'df' shows free space, leading to missed cleanup opportunities.
Quick: Can a script using 'df' alone detect disk inode exhaustion? Commit yes or no.
Common Belief:Many think disk space monitoring scripts automatically detect all disk full conditions including inode limits.
Tap to reveal reality
Reality:'df' reports space usage but inode exhaustion (running out of file entries) requires checking 'df -i' or other tools separately.
Why it matters:Ignoring inode limits can cause unexpected failures even if disk space is available.
Quick: Is it safe to run disk monitoring scripts as root always? Commit yes or no.
Common Belief:Some believe running scripts as root is necessary and safe for disk monitoring.
Tap to reveal reality
Reality:Running scripts as root increases risk if the script has bugs or security flaws; running as a normal user with proper permissions is safer.
Why it matters:Running as root unnecessarily can lead to accidental system damage or security breaches.
Expert Zone
1
Disk usage can spike temporarily due to log rotations or backups; scripts should handle transient changes gracefully.
2
Parsing 'df' output can vary across Unix flavors; robust scripts use consistent field extraction or tools like 'stat' for accuracy.
3
Alert fatigue happens if thresholds are too low or alerts too frequent; tuning thresholds and alert frequency is critical in production.
When NOT to use
Disk space monitoring scripts are not suitable for real-time high-frequency monitoring or complex storage systems like distributed filesystems. For those, specialized monitoring tools like Prometheus with node exporters or enterprise storage management software are better choices.
Production Patterns
In production, disk monitoring scripts run as cron jobs with logging and alert integration via email or messaging platforms like Slack. They often include multiple thresholds (warning, critical) and handle multiple disks and mount points. Logs are rotated and archived for audit and trend analysis.
Connections
System Logging
Builds-on
Understanding disk monitoring helps grasp how system logs grow and why log rotation is important to prevent disk full errors.
Automated Alerting Systems
Builds-on
Disk monitoring scripts form the foundation for automated alerting, teaching how simple checks trigger notifications in larger monitoring setups.
Supply Chain Inventory Management
Analogy in different field
Just like disk monitoring tracks storage space to avoid shortages, inventory management tracks stock levels to prevent running out of goods, showing a universal pattern of resource monitoring.
Common Pitfalls
#1Ignoring script permissions causing it not to run or send alerts.
Wrong approach:./check_disk.sh # Script runs but alert commands fail silently due to lack of permissions
Correct approach:chmod +x check_disk.sh ./check_disk.sh # Script runs and alerts work because permissions are set correctly
Root cause:Not setting executable permissions or lacking rights to send alerts causes silent failures.
#2Parsing 'df' output incorrectly leading to wrong usage values.
Wrong approach:usage=$(df -h / | awk '{print $5}') # This prints header and value, causing errors
Correct approach:usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//') # Correctly extracts only the usage percentage number
Root cause:Not skipping header line or cleaning output leads to wrong data in comparisons.
#3Hardcoding disk names causing script to miss other disks.
Wrong approach:usage=$(df -h / | awk 'NR==2 {print $5}') # Only checks root disk, ignores others
Correct approach:df -h | tail -n +2 | while read line; do usage=$(echo "$line" | awk '{print $5}' | sed 's/%//') # check usage for each disk done
Root cause:Assuming only one disk or fixed disk name limits script usefulness.
Key Takeaways
Disk space monitoring scripts automate checking storage usage to prevent system failures.
Parsing command output precisely is essential for accurate monitoring and alerting.
Scheduling scripts with cron ensures continuous, hands-free disk monitoring.
Handling multiple disks and logging usage history improves system insight and troubleshooting.
Understanding limitations and common pitfalls helps build reliable and effective monitoring tools.