2

I am trying to find the top 10 processes which are consuming more CPU and Memory using the command

ps -Ao user,uid,comm,pid,pcpu,pmem --sort=-pcpu | head -n 10

But it's not showing the correct data as I could see for the same process the CPU% value fluctuate in top command like 10% and then 250%.

Please help to list the high CPU consumption process by considering all cores.

We cannot install any tool as its a Prod Server. To clarify, the requirement is whenever there is alert on high CPU usage on the server we are trying to capture the top 10 processes along with CPU and Memory and send it as mail. If we extract only top output at that time it may not show the usage of process across cores as it will fluctuate.

5
  • 1
    Do the processes just "bounce" around, using lots of CPU one second and very little the next? Not much you can do about that, except maybe take an average CPU over time
    – Xen2050
    Oct 13, 2018 at 4:33
  • Can you help me to get the Average CPU over time for top 10 processes
    – Thangan
    Oct 13, 2018 at 4:35
  • I'd just watch top or htop, not sure how to do that in a script though, I'd search for it (seems like it should be common)
    – Xen2050
    Oct 13, 2018 at 4:38
  • 1
    Actually, we don't have htop... The requirement is whenever there is alert on high CPU usage on the server we are trying to capture the top 10 processes along with CPU and Memory and send it as mail...If we extract top output at that time it may not show the usage of process across cores as it will fluctuate... so stuck with that situation and need your help...
    – Thangan
    Oct 13, 2018 at 4:50
  • Sounds like the sysstat tools might be a better choice than trying to find a need in a haystack.
    – Tigger
    Oct 13, 2018 at 23:47

2 Answers 2

1

There seems to be a spelling error in your sort flag (-pcpc should be -pcpu)

Anyways, running the corrected command yields the following snapshot showing combined CPU usage for the top 9 processes sorted descending by cpu usage (use head -n 11 to get the top 10):

$ ps -Ao user,uid,comm,pid,pcpu,pmem --sort=-pcpu | head -n 10
USER       UID COMMAND           PID %CPU %MEM
husjon    1000 PathOfExile_x64 13233  225  4.7
husjon    1000 wineserver      13172 30.4  0.0
husjon    1000 chrome          12501 18.7  1.3
husjon    1000 chrome          10631 13.2  0.8
husjon    1000 lutris          12474  7.6  0.2
root         0 Xorg              620  5.6  0.1
husjon    1000 compton          2146  4.2  0.2
husjon    1000 chrome          10595  4.1  1.0
husjon    1000 chrome          32332  3.4  0.5

Hope this helps

2
  • Corrected my command(typo)..The problem here is it is showing the CPU usage of a process for particular core as i could see same process running in another core with very high CPU usage.... Is there any way to show the CPU usage of particular process across core?
    – Thangan
    Oct 17, 2018 at 2:24
  • Sorry for the late reply. If you're seeing multiples of a single process name (in ps), that would be another instance of that application unless specified to show each thread. Adding the -L flag, allows you to show the threads. This can be verified by adding tid to the column list when using the -L flag, if the TID and PID per line is the same value, this indicates an induvidual process. A multi-threaded process will have repeating PID, however varying TIDs.
    – husjon
    Oct 19, 2018 at 14:52
0

You may use the top command to monitor processes and system resource usage on Linux. It is one of the most useful tools in a sysadmin’s toolbox, and it comes pre-installed on every distribution. Unlike other commands such as ps, it is interactive, and you can browse through the list of processes, kill a process, and so on.

For examples of its use, see the article 12 TOP Command Examples in Linux.

1
  • htop is, I guess, a better alternative.
    – x-yuri
    Jan 13, 2022 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .