Project for DevOps roadmap on roadmap.sh
https://roadmap.sh/projects/server-stats
A Bash script that prints a quick snapshot of CPU, memory, and disk usage, along with the top 5 processes by CPU and memory consumption.
- CPU usage — overall current usage, calculated as
100% - idle% - Memory usage — total / used / free (including cache/buffers as reclaimable), with percentage used
- Disk usage — total / used / available across all mounted filesystems, with percentage used
- Top 5 processes by CPU usage — PID, command, and CPU%
- Top 5 processes by memory usage — PID, command, and MEM%
All sections are computed from a single cached top -bn2 snapshot, so the script only pays the top sampling delay once.
This script was built and tested on a standard Linux desktop/server setup and depends on the following:
| Tool | Required version / notes |
|---|---|
bash |
Any modern version |
top |
procps-ng flavor (standard on Debian, Ubuntu, Fedora, RHEL/CentOS, Arch). BusyBox top (Alpine, minimal containers) is not supported — different output format. |
free |
procps-ng version with an available column (v3.3.10+, i.e. most systems from ~2014 onward) |
df |
Standard GNU coreutils df |
awk |
Any POSIX-compliant awk (gawk, mawk, BusyBox awk all work — no gawk-only features used) |
- Alpine Linux or other BusyBox-based systems (different
topoutput format) - Very old distros with legacy
free(noavailablecolumn) - Minimal containers without
bashinstalled
- Clone the repository
- Make it executable:
chmod +x server-stats.sh
- Run it:
./server-stats.sh
No arguments or flags — it just runs and prints the full report.
----------------------CPU usage:----------------------
Your current CPU usage is 4.3%
----------------------Memory usage:----------------------
You are using 47.03% of your memory
Total: 14.94Gi
Used: 7.04Gi
Free: 7.90Gi
----------------------Disk usage:----------------------
You are using 62.15% of your disk
Size: 512.00G
Used: 318.20G
Available: 193.80G
----------------------Top 5 processes by CPU usage:----------------------
Usage PID Command
1. 9.1% 10998 Discord
2. 9.1% 57966 top
3. 0.0% 1 systemd
4. 0.0% 2 kthreadd
5. 0.0% 3 pool_wo+
----------------------Top 5 processes by memory usage:----------------------
Usage PID Command
1. 5.6% 5388 brave
2. 3.5% 11367 Discord
3. 1.4% 10998 Discord
4. 0.1% 1 systemd
5. 0.0% 57966 top
- CPU snapshot accuracy:
top -bn2takes two samples ~3 seconds apart and uses the second (more accurate) one for the top-5 lists. The single-line "current CPU usage" figure is drawn from the same second sample. - Colored/bold output uses ANSI escape codes — these render correctly in a normal terminal but will appear as raw escape characters if output is redirected to a file or a non-ANSI-aware destination (e.g. some log viewers,
cronmail).
- Output looks wrong or empty: check your
topflavor withtop --version— it should say "procps-ng". If not, the script's column-position logic won't match yourtop's output format. - Memory section errors out: check
free -m— the header row should include anavailablecolumn. If it doesn't, yourfreeversion is too old for this script as written.