Disk Usage Monitor Bash Script for Linux Systems

marius

Administrator
Staff member
Introduction:
This Bash script monitors disk usage on your Linux system and alerts you when any filesystem exceeds a specified usage threshold. It is useful for preventing issues related to disk space exhaustion and helps keep your system running smoothly.

Bash:
#!/bin/bash

# Set the threshold for disk usage (in percentage)
THRESHOLD=80

# Process the output of 'df -h' for each filesystem
while read filesystem size used avail percent mp; do
    # Remove the percentage sign
    usage=${percent%\%}
    # Check if usage exceeds the threshold
    if (( usage > THRESHOLD )); then
        echo "Warning: Filesystem $filesystem running at ${percent} usage."
    fi
done < <(df -h | tail -n +2)

exit 0

Code:
# How to use this script:
1. Save the code as disk_usage_monitor.sh
2. Make the script executable with: chmod +x disk_usage_monitor.sh
3. Run the script by executing: ./disk_usage_monitor.sh

Code:
# Example output when a filesystem exceeds the threshold:
Warning: Filesystem /dev/sda1 running at 85% usage.
 
Back
Top