Print 10 most CPU-intensive processes (wo/ top)

ps -eo pid,comm,%cpu

lists all processes (in increasing PID number).

How to get only the top-10 most CPU intensive ones? I know about top: this is BASH exercise.

I tried redirecting above code to cut

ps -eo pid,comm,%cpu | cut -f2

but ps' output isn't TAB delimited. How can I otherwise use sort, if I can't select the 3rd (%CPU) field?

EDIT:
If using only the 3rd field (%cpu), then this will do:

ps -eo %cpu | sort -r

But now, how can I retrieve the PIDs? :confused:

What about something like:

ps -eo pid,comm,%cpu | awk '{OFS="|"; print $1,$2,$3}'

which would then get you a file with '|' character to use as a field separator.
That might make it easier to sort, grep, etc...

1 Like

First you need the processes and the information you want, then you have to format the output.

On HP-UX I use:

   export UNIX95=1
   ps -efH -o pcpu,user,nice,cpu,pid,args |  grep -vE "^ *0\.0|defunct|CPU|high_cpu|init" >$LOG_FILE1

Then you need to 'sort' the information:

sort -T $SORT_DIR -r -n -k 1

And if you only want the top 10, use

head

Finally:
Ex:

high_cpu

CPU    User       Nice   CPU  PID        Command
------------------------------------------------------------------------------------------------------------------------------------
96.07  root       20     244  2453       /opt/wbem/lbin/cimprovagt 0 5 10 root SFMProviderModule
25.82  brm01dev   22     209  17528      oracledps01dvu (LOCAL=NO)
19.47  brm01dev   22     228  2669       oracledps02dvu (LOCAL=NO)
4.59   brm01dev   22     28   18894      oracledps01dvu (LOCAL=NO)
3.83   brm01dev   22     26   18508      oracledps01dvu (LOCAL=NO)
3.64   dpsdev     22     27   8961       oraclegendvu (LOCAL=NO)
2.11   brm01dev   22     6    17805      oracledps01dvu (LOCAL=NO)
1.66   pasdev     22     17   3499       oraclepasdvu (LOCAL=NO)
1.33   root       20     0    80         pagezerod
1.23   brm01dev   22     13   13796      /oracle/pasdev/app/product/10.2.brm/bin/tnslsnr listenerbrm01dev -inherit
1.03   brm01dev   22     10   8500       oracledps02dvu (LOCAL=NO)
1.01   brm01dev   22     8    2093       ora_dbw0_dps02dvu
0.98   brm01dev   22     29   8476       oracledps02dvu (LOCAL=NO)
0.78   root       20     0    3023       /opt/perf/bin/midaemon
0.65   root       20     9    8407       sshd: purdym [priv]
0.58   root       20     0    3294       /usr/lbin/utild

1 Like

Brilliant. Seems like I'll have to take a look at awk. Thanks.

EDIT: OK, seems like you can make it with grep also ... but what's the role of UNIX95 variable?
For my current level, I'll just combine awk and filter out defunct zombies with grep.

---------- Post updated at 03:58 PM ---------- Previous update was at 03:39 PM ----------

Wait ... after awk ( ps -eo pid,comm,%cpu | awk '{OFS="|"; print $1,$2,$3}' ), how do I sort according to $3 field?

If I cut the 3rd field, I lose the PIDs.

---------- Post updated at 04:04 PM ---------- Previous update was at 03:58 PM ----------

There is a simpler solution ... sort's -k option:

ps -eo pid,comm,%cpu | sort -rk 3 | head

:stuck_out_tongue:

1 Like

You can sort on any field, or sort defaults to starting at the left.
So, you an keep in the current layout of three (or more/less) fields, and then work with the various sort options. You would specify the a -t option noting the '|' as delimiter. Check into the -n to specify numerics.

Or, awk can print in a different order. You could:

ps -eo pid,comm,%cpu | awk '{OFS="|"; print $3,$1,$2}'

and put the third field first.

Note:
I can see that you are experimenting, and that is good. Where I am right now I am having difficulty recreating your ps command, thus the suggestions are all from memory.

1 Like

From post #1.

Sorry, but I don't believe that this line is syntactically correct (because of the "%" character) - but I could be wrong.
What Operating System and version you you have? There is much variation in the "ps" command.

Assuming that the syntax is correct on your system (which I do not believe), the "-o" parameter to "ps" allows you to display the parameters in any order.
We don't need "awk" to change the order of the fields. I note that "purdym" knows this.

ps -eo %cpu,pid,comm | sort -nr | head -10

My version of the HP-UX "Top 10 CPU users" includes:

UNIX95= ps -e -o pcpu -o ruser -o args|sort -nr|grep -v %CPU|head -10 

This can equally be:

UNIX95= ps -e -o pcpu,ruser,args|sort -nr|grep -v %CPU|head -10 

The "UNIX95" variable is specific to HP-UX and causes certain commands to behave with Berkeley unix syntax rather than Unix System V syntax. The manual suggests that the default "ps" syntax is "Posix" but that version is inferior to the Berkeley version.