Build a Simple Bash Script to Monitor CPU, Memory, and Disk Usage on Linux
This tutorial walks you through creating a Bash script that periodically captures and displays memory, disk, and CPU utilization on a Linux server, explains the underlying commands, shows how to run the script in a timed loop, and demonstrates adding a stress test for load generation.
Monitoring system resources is essential for server operations, especially when deploying new applications. This guide shows how to write a compact Bash script that reports memory, disk, and CPU usage as three columns.
1. Monitoring Memory
free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'The free -m command displays total and used memory. Using awk we extract the second line, where $3 is used memory and $2 is total memory, then calculate the usage percentage.
2. Monitoring Disk
df -h | awk '$NF=="/"{printf "%s\t\t", $5}' df -hreports filesystem usage. The awk filter selects the line whose mount point is / and prints the fifth field, which contains the used‑percentage (e.g., 7%).
3. Monitoring CPU
top -bn1 | grep load | awk '{printf "%.2f%%\t\t
", $(NF-2)}' top -bn1runs top in batch mode for a single iteration. grep load extracts the line with the load averages, and awk prints the field two positions before the end, which corresponds to the current CPU load percentage.
4. Assembling the Script
#! /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
doneThe script prints a header, then enters a while loop that runs for one hour ( 3600 seconds). Each iteration updates the three variables, echoes them as a tab‑separated line, and pauses five seconds.
5. Optional Logging
You can redirect the script output to a log file:
./stats.sh >> log.txt6. Stress Testing (Optional)
To generate load for testing, install stress (e.g., on CentOS) and run a one‑hour stress test:
# yum install stress
# stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 3600sWhile the stress test runs, the monitoring script will show increasing CPU and memory percentages.
Running the script alongside the stress test produces output such as:
Memory Disk CPU
20.48% 7% 1.21%
20.48% 7% 1.02%
... (continues every 5 seconds)This completes a practical Bash tutorial for system administrators to monitor key resource metrics continuously.
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.
