Operations 9 min read

Build a Bash Script to Monitor Memory, Disk, and CPU in Real Time

This tutorial shows how to create a Bash script that continuously reports memory, disk, and CPU usage percentages, logs the data, and optionally applies a stress test to evaluate system performance under load.

Open Source Linux
Open Source Linux
Open Source Linux
Build a Bash Script to Monitor Memory, Disk, and CPU in Real Time

Monitoring the environment is essential for server operations, especially when deploying new applications. While many open‑source tools exist for continuous monitoring, a simple Bash script can be used for short‑term testing.

The script outputs a three‑column table showing the percentage of memory, disk, and CPU usage on the host.

1. Monitor Memory

free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'

The free -m command displays total and used memory; awk extracts the second line and calculates used memory as a percentage of total.

2. Monitor Disk

df -h | awk '$NF=="/"{printf "%s\t\t", $5}'
df -h

reports filesystem usage; the awk filter selects the line where the mount point is / and prints the usage column.

3. Monitor CPU

top -bn1 | grep load | awk '{printf "%.2f%%\t\t
", $(NF-2)}'
top -bn1

runs top in batch mode for a single iteration; grep load extracts the line containing the load average, and awk prints the load value as a percentage.

These commands are stored in variables:

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)}')

A while loop runs for a defined period (e.g., one hour), printing the three values every five seconds.

end=$((SECONDS+3600))
while [ $SECONDS -lt $end ]; do
  echo "$MEMORY$DISK$CPU"
  sleep 5
done

The complete script is:

#! /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
done

Running the script prints lines such as:

Memory\t\tDisk\t\tCPU
9.34%\t7%\t0.00%
9.34%\t7%\t0.00%
...

Output can be redirected to a log file for later analysis.

Stress Test

Stress test diagram
Stress test diagram

To generate load, install the stress utility (e.g., yum install stress) and run: stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 3600s This creates two CPU‑intensive processes, one I/O‑intensive process, and one memory allocator process for one hour, allowing you to observe how the monitoring script reports increased resource usage.

The tutorial ends here, providing a simple yet useful Bash script for system administrators.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Bashshell scriptstress testresource usage
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.