BASH
Display System Resource Usage
Get a quick, human-readable overview of your system's CPU, memory, and disk usage with this simple yet effective bash script. Essential for monitoring server health.
#!/bin/bash
echo "--- System Resource Overview ---"
# --- CPU Usage ---
echo "
CPU Usage:"
# Get total CPU usage (excluding idle) from top
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%id.*/\1/g" | awk '{print 100 - $1}')
printf " Total: %.2f%%
" "$CPU_USAGE"
# --- Memory Usage ---
echo "
Memory Usage:"
# Get memory stats in human-readable format from free
free -h | awk '/^Mem:/ {printf " Total: %s
Used: %s
Free: %s
Available: %s
", $2, $3, $4, $7}'
# --- Disk Usage (for root filesystem) ---
echo "
Disk Usage (Root /):"
# Get disk usage for the root filesystem from df
df -h / | awk 'NR==2 {printf " Total: %s
Used: %s
Available: %s
Usage: %s
", $2, $3, $4, $5}'
# --- Uptime ---
echo "
System Uptime:"
uptime -p
echo "--------------------------------
"
How it works: This script provides a concise summary of critical system resources. It reports the current CPU utilization, breaks down memory usage into total, used, free, and available, and displays the disk usage for the root filesystem, all in human-readable formats. Additionally, it shows the system uptime. This consolidated information is extremely useful for a quick health check of a server or development environment, helping to identify potential performance bottlenecks or resource exhaustion issues at a glance.