Build a Simple Bash Script to Monitor CPU, Memory, and Disk Usage on Linux
This tutorial walks you through creating a concise Bash script that periodically captures memory, disk, and CPU utilization on a Linux server, displays the results in a three‑column table, logs the data, and optionally applies a stress test to generate load.
Introduction
Monitoring system resources is essential for server administration, especially when deploying new applications. This guide shows how to write a Bash script that reports memory, disk, and CPU usage percentages in a three‑column format.
Memory Monitoring
The free -m command provides total and used memory. Using awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2}' extracts the used‑memory percentage from the second line.
free -m # free -m
total used free shared buffers cached
Mem: 996 92 904 0 11 31
-/+ buffers/cache: 49 947
Swap: 1583 0 1583Disk Monitoring
The df -h command reports filesystem usage. The following pipeline isolates the root partition and prints its usage percentage:
df -h | awk '$NF=="/"{printf "%s\t\t", $5}' # df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 14G 814M 12G 7% /
...CPU Monitoring
Using top -bn1 in batch mode, we extract the load average and convert it to a percentage:
top -bn1 | grep load | awk '{printf "%.2f%%\t\t
", $(NF-2)}' # top -bn1 | grep load
top - 19:31:25 up 1:47, 1 user, load average: 0.00, 0.00, 0.00Assembling the Script
Store each metric in a variable, then loop for a defined period (e.g., one hour) printing the values every few seconds.
#!/bin/bash
printf "Memory\t\tDisk\t\tCPU
"
end=$((SECONDS+3600))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2}')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t
", $(NF-2)}')
echo "$MEMORY$DISK$CPU"
sleep 5
doneRunning the script prints lines such as:
Memory Disk CPU
9.34% 7% 0.00%You can redirect the output to a log file with ./stats.sh >> log.txt.
Stress Testing (Optional)
Install the stress utility (e.g., yum install stress) and run a load test to generate CPU and memory pressure for an hour:
# stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 3600sWhile the stress test runs, the monitoring script continues to report resource usage, allowing you to observe system behavior under load.
Signed-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.
