DEV Community

Cover image for 💀 I built a full system monitor in Bash — and fought awk along the way
DanLin
DanLin

Posted on

💀 I built a full system monitor in Bash — and fought awk along the way

I wanted to see how far I could push pure Bash before it collapses under its own syntax.
So, naturally, I decided to write a system monitoring tool — in Bash.
And thus, system-monitor was born:
👉 GitHub repo

Yes, it’s fully functional. Yes, it uses colors. Yes, awk is involved.

No, I don’t recommend doing this sober. 😅


💡 The idea

I wanted a single script that could show:

  • CPU load and number of cores
  • RAM usage and percentage
  • Disk space for /
  • Network I/O
  • Process count Basically, the “lazy Linux admin toolkit” in one file. Something like this:
./system-monitor.sh
Enter fullscreen mode Exit fullscreen mode

and boom — everything you’d usually get from top, free, df, and ip combined.


⚙️ The features

What started as a 10-line script turned into a 250+ line CLI tool with:

  • Command-line arguments (--help, --brief, --no-color, --version, -i N)
  • Colorized output for warnings/critical thresholds
  • Continuous monitoring mode
  • Safe shutdown with signal trapping (Ctrl+C)
  • Brief (machine-readable) mode for piping into other scripts
  • Dependency checks and graceful error handling

Example:

./system-monitor.sh -i 5 --no-color
Enter fullscreen mode Exit fullscreen mode

Updates every 5 seconds without colors.
Press Ctrl+C to stop — yes, it even says goodbye politely.


🧩 Some code highlights

Color management:

RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m'
Enter fullscreen mode Exit fullscreen mode

Because monitoring your system is serious business — but who doesn’t love a bit of RGB?

Load detection magic:

CPU_LOAD_1MIN=$(cat /proc/loadavg | awk '{print $1}')
CPU_CORES=$(nproc)
load_percent=$(echo "scale=0; ($CPU_LOAD_1MIN * 100) / $CPU_CORES" | bc)
Enter fullscreen mode Exit fullscreen mode

That moment when you realize you’re using awk, bc, and /proc in one line and it actually works.

Network metrics:

interface=$(ip route get 8.8.8.8 | awk '{print $5}' | head -1)
rx=$(cat /sys/class/net/$interface/statistics/rx_bytes)
tx=$(cat /sys/class/net/$interface/statistics/tx_bytes)
Enter fullscreen mode Exit fullscreen mode

I like to imagine Bash crying softly every time it runs this.


🧠 What I learned

  • Bash can do a lot — if you’re patient (and slightly masochistic).
  • Quoting is a survival skill. Forget one and you’re debugging for hours.
  • awk is an ancient curse. It always works, but never for the reason you expect.
  • Color helps debugging. Visual feedback saves your sanity.
  • Document your code. Because 3 a.m. you will have no clue what that $7 in awk '{print $7}' was.

🧰 The brief mode

I added a --brief flag that outputs all metrics in a machine-readable format — perfect for logging or Prometheus-style integration.

./system-monitor.sh --brief
Enter fullscreen mode Exit fullscreen mode

Output:

timestamp=1730765432
cpu_load_percent=12
mem_usage_percent=43.5
disk_usage_percent=56
network_rx_mb=12.4
process_count=187
Enter fullscreen mode Exit fullscreen mode

Basically, JSON for people who hate themselves.


🧯 Safety first: signal handling

If you hit Ctrl+C, it doesn’t just die — it politely says goodbye:

trap cleanup INT TERM

cleanup() {
    echo -e "\nMonitoring stopped. Goodbye!"
    exit 0
}
Enter fullscreen mode Exit fullscreen mode

Because professionalism.


🚀 AUR packaging

After testing it on Arch, I thought — why not go all in?
So I wrote a PKGBUILD, threw it into the AUR, and now you can literally install it with:

yay -S system-monitor
Enter fullscreen mode Exit fullscreen mode

And that’s the moment I realized:

“Oh no. I’ve become that guy who writes Bash utilities for other people.”


🧭 Final thoughts

Was this efficient? Probably not.
Did I learn a lot? Absolutely.

Here’s what I got from it:

  • Deep understanding of Bash syntax and traps
  • Practical use of /proc and awk
  • Appreciation for proper CLI design
  • And a newfound respect for people who don’t do this in Bash

If you’re thinking of writing your first utility — do it.
Even if it’s messy. Even if awk haunts your dreams.

Because once you see your tool working, it’s worth every headache.


🔗 Links

🐧 GitHub: https://github.com/DanLinX2004X/system-monitor

📦 AUR: https://aur.archlinux.org/packages/system-monitor

Top comments (0)