How to Build a Bash Script for Real‑Time Linux CPU, Memory, and Disk I/O Monitoring
This article provides a complete Bash script that gathers Linux system metrics—including IP address, CPU core count, utilization percentages, load averages, memory and swap usage, and disk I/O statistics—while explaining each command (ifconfig, grep, awk, top, vmstat, free, iostat, uptime) and how the script assembles the results.
Script
The following Bash script gathers common Linux performance metrics: IP address, CPU core count, CPU utilisation, interrupt and context‑switch statistics, load averages, memory and swap usage, and per‑device disk I/O statistics.
#!/bin/bash
# 1. Local IP address
IP=$(ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}')
echo "IP address: $IP"
# 2. CPU core count
cpu_num=$(grep -c "model name" /proc/cpuinfo)
echo "CPU cores: $cpu_num"
# 3. CPU utilisation (top in batch mode)
cpu_user=$(top -b -n 1 | grep Cpu | awk '{print $2}' | cut -d% -f1)
echo "User CPU %: $cpu_user"
cpu_system=$(top -b -n 1 | grep Cpu | awk '{print $4}' | cut -d% -f1)
echo "System CPU %: $cpu_system"
cpu_idle=$(top -b -n 1 | grep Cpu | awk '{print $8}' | cut -d% -f1)
echo "Idle CPU %: $cpu_idle"
cpu_iowait=$(top -b -n 1 | grep Cpu | awk '{print $10}' | cut -d% -f1)
echo "I/O wait CPU %: $cpu_iowait"
# 4. Interrupts, context switches and run‑queue length (vmstat)
cpu_interrupt=$(vmstat -n 1 1 | sed -n 3p | awk '{print $11}')
echo "CPU interrupts: $cpu_interrupt"
cpu_context=$(vmstat -n 1 1 | sed -n 3p | awk '{print $12}')
echo "CPU context switches: $cpu_context"
cpu_runqueue=$(vmstat -n 1 1 | sed -n 3p | awk '{print $1}')
echo "CPU run‑queue length: $cpu_runqueue"
# 5. Load averages (uptime)
load_1=$(uptime | awk -F'[,:]' '{print $2}' | tr -d ',')
echo "Load average (1 min): $load_1"
load_5=$(uptime | awk -F'[,:]' '{print $3}' | tr -d ',')
echo "Load average (5 min): $load_5"
load_15=$(uptime | awk -F'[,:]' '{print $4}' | tr -d ',')
echo "Load average (15 min): $load_15"
# 6. Memory and swap (free)
mem_total=$(free | grep Mem | awk '{print $2}')
echo "Total physical memory: $mem_total"
mem_used=$(free | grep Mem | awk '{print $3}')
echo "OS used memory: $mem_used"
mem_free=$(free | grep Mem | awk '{print $4}')
echo "OS free memory: $mem_free"
app_used=$(free | sed -n 3p | awk '{print $3}')
echo "Application used memory: $app_used"
app_free=$(free | sed -n 3p | awk '{print $4}')
echo "Application free memory: $app_free"
swap_total=$(free | grep Swap | awk '{print $2}')
echo "Total swap: $swap_total"
swap_used=$(free | grep Swap | awk '{print $3}')
echo "Used swap: $swap_used"
swap_free=$(free | grep Swap | awk '{print $4}')
echo "Free swap: $swap_free"
# 7. Disk I/O statistics for /dev/sda (iostat from sysstat)
echo "Statistics for device /dev/sda"
read_req=$(iostat -kx | grep sda | awk '{print $4}')
echo "Read requests per second: $read_req"
write_req=$(iostat -kx | grep sda | awk '{print $5}')
echo "Write requests per second: $write_req"
avg_queue=$(iostat -kx | grep sda | awk '{print $9}')
echo "Average queue length: $avg_queue"
await=$(iostat -kx | grep sda | awk '{print $10}')
echo "Average request time (ms): $await"
service=$(iostat -kx | grep sda | awk '{print $11}')
echo "Service time (ms): $service"
util=$(iostat -kx | grep sda | awk '{print $12}')
echo "CPU utilisation for I/O: $util"Explanation of the script
1. Obtaining the local server IP address
Run ifconfig | grep inet to list lines containing the string inet.
Exclude IPv6 and the loopback address with grep -vE 'inet6|127.0.0.1'.
Extract the second column (the IP) using awk '{print $2}' and assign it to IP.
Print the result with echo.
2. Counting CPU cores
The file /proc/cpuinfo lists each core on a line containing model name.
Use grep -c "model name" /proc/cpuinfo to count those lines, yielding the total core count.
3. CPU utilisation
The top command in batch mode ( -b -n 1) outputs a single snapshot that can be parsed.
Filter the line containing Cpu with grep Cpu.
Extract the desired column (user, system, idle, iowait) using awk.
Remove the trailing percent sign with cut -d% -f1.
4. CPU interrupts, context switches and run‑queue length
The vmstat -n 1 1 command produces one snapshot without headers.
Use sed -n 3p to select the data line.
Field 11 gives interrupt count, field 12 gives context‑switch count, and field 1 gives the run‑queue length; extract them with awk.
5. Load averages
The uptime command prints the 1‑, 5‑ and 15‑minute load averages. The script splits the output with awk -F'[,:]' and removes trailing commas.
6. Memory and swap information
The free command reports physical memory and swap usage.
Parse the line starting with Mem to obtain total, used and free memory (columns 2‑4).
Parse the third line of free (produced by sed -n 3p) for application‑level used and free memory.
Parse the line starting with Swap for total, used and free swap values.
7. Disk I/O statistics
The iostat -kx command (from the sysstat package) provides per‑device I/O metrics.
Filter the output for the target device (e.g., sda) with grep sda.
Extract the following fields with awk:
Column 4 – read requests per second
Column 5 – write requests per second
Column 9 – average queue length
Column 10 – average request time (ms)
Column 11 – service time (ms)
Column 12 – CPU utilisation for I/O (%)
On Debian/Ubuntu systems, install the required sysstat package before using iostat:
sudo apt-get install sysstatSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
