Linux : See CPU usage by a process for the last second

linuxmonitoringprocessps

vmstat 1

Above will print virtual memory statistics each seconds. It will also show the CPU utilization for last second.

I have a web server at hand which runs httpd and MySQL. I need to find how much CPU httpd consumed in last second. Like vmstat particularly for httpd.

I tried this :

ps -e -o %mem,%cpu,cmd | grep mysql | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'

But it will show me the ratio of CPU used since the start of the process.

So, with above, if my process caused a spike and then went to sleep for long time, I won't know it. It's like windows process manager, which shows which process is using how much CPU. I hope I am making my question understandable. I will clarify if anything is missing.

Best Answer

You could use top -b -d 1 to achieve that for CPU usage. top displays process CPU usage relative to the last output.

Related Question