Advertisement
Monitoring & Log Management

Scripting CPU and RAM Usage Reports on Enterprise Ubuntu

cpu usage bash script memory monitor ubuntu server health report

Stop Guessing Why Your Server Crashed

Close up, cinematic lighting, a stressed sysadmin staring at a glowing red server rack in a dark data center, cyberpunk aesthetic, 8k resolution, photorealistic --ar 16:9

We've all been there. It's 3 AM. The pager goes off. Your enterprise Ubuntu box is completely unresponsive. You reboot it, cross your fingers, and pretend everything is fine. But it isn't fine. Without a baseline server health report, you are flying blind. Let's fix that. No expensive SaaS tools required. Just you, the terminal, and some basic shell logic.

Advertisement

Nailing Down the CPU Hogs

A glowing green terminal screen showing lines of code, matrix style but modern, focused depth of field, minimalist hacker desk setup, hyperdetailed --ar 16:9

Writing a reliable cpu usage bash script isn't rocket science. Actually, it's mostly just knowing how to parse the `top` or `mpstat` command. Grab the idle percentage, subtract it from 100, and you have your usage. Throw that into a variable. Done. The trick is consistency. You want to sample the CPU over a few seconds, not grab a single microsecond snapshot. That prevents sudden, meaningless spikes from ruining your data.

Memory Leaks Are the Enemy

A neon-lit gauge meter dropping to zero, warning signs, abstract digital interface, sleek UI design, high contrast, vibrant colors --ar 16:9

RAM issues will kill your server faster than anything else. The OOM (Out of Memory) killer wakes up and starts murdering your database processes. Brutal. Building a memory monitor ubuntu script requires understanding how `free -m` actually works. Don't look at the "free" memory column. Look at "available" memory. Linux caches aggressively. Your script needs to parse that available column and sound the alarm when it dips below a safe threshold. Like 20 percent.

Gluing It Together Into a Dashboard

Individual scripts are useless if you have to run them manually. Combine your CPU and RAM logic. Spit the output into a clean CSV format. Include a timestamp. Always include a timestamp. Now you have the foundation of a proper server health report. Push it to a local log file. If you are feeling ambitious, pipe that output directly into a Slack webhook. Let the server tell you it's dying before the clients complain.

Automate and Walk Away

Cron is your best friend here. Set your newly minted script to run every five minutes. `*/5 * * * * /path/to/script.sh`. That's it. You now have a historical record of your machine's vitals. Just remember to set up log rotation for your report file using `logrotate`. Otherwise, your tiny monitoring script will eventually consume your entire hard drive. Which is deeply ironic.

Advertisement