Sum up CPU and Memory for a whole service, not just individual processes

Process & Resource Monitoring (Top/Htop/Ps/Systemd)

Sum up CPU and Memory for a whole service, not just individual processes

🧩 The Challenge

Server’s feeling sluggish, you run `top`, and see a bunch of `php-fpm` or `java` processes, each taking a bit of CPU or memory. But how do you quickly figure out the *total* resources that entire stack is gobbling up? Scrolling through `top` and mentally adding percentages is a special kind of hell. You just want the bottom line for that service.

💡 The Fix

Grab `ps` output, filter for what you need. And then let `awk` do the heavy lifting of summing up the CPU and memory columns. You’ll get a quick, accurate total for all those related processes, instead of squinting at individual lines.

ps -eo pid,pcpu,pmem,args | grep 'php-fpm' | grep -v grep | awk '{cpu+=$2; mem+=$3} END {print "Total CPU: "cpu"% \nTotal MEM: "mem"%"}'

⚙️ Why It Works

This works by telling `ps` exactly what columns you want (PID, CPU, Memory, command). Next, `grep` filters it down to just the processes you care about. Finally, `awk` iterates through those lines, adding up the values in the CPU and Memory columns, giving you a grand total. It’s way faster than manual calculation.

🚀 Pro-Tip: Need to check a specific user? Swap out the `grep ‘php-fpm’` for `ps -U <username>` for a quick user-level aggregate.

Linux Tips & Tricks | © ngelinux.com | 11/3/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted