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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print 10 most CPU-intensive processes (wo/ top)
# 1  
Old 01-24-2011
Question 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?

If using only the 3rd field (%cpu), then this will do:
ps -eo %cpu | sort -r

But now, how can I retrieve the PIDs? Smilie

Last edited by courteous; 01-24-2011 at 05:17 PM..
# 2  
Old 01-24-2011
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...
This User Gave Thanks to joeyg For This Post:
# 3  
Old 01-24-2011
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


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

This User Gave Thanks to purdym For This Post:
# 4  
Old 01-24-2011

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


Last edited by courteous; 01-24-2011 at 05:45 PM..
This User Gave Thanks to courteous For This Post:
# 5  
Old 01-24-2011
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.

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.

Last edited by joeyg; 01-24-2011 at 06:06 PM.. Reason: Added a final note.
This User Gave Thanks to joeyg For This Post:
# 6  
Old 01-24-2011
From post #1.
ps -eo pid,comm,%cpu
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.

Last edited by methyl; 01-24-2011 at 07:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Record top accessed processes/files

Hello, I have about 100 servers that I'm looking to collect information regarding top files and processes accessed within a 168 hr (1 week) period. Each server has a different purpose and so different installed applications. All servers are running either unix or linux. What would be a... (0 Replies)
Discussion started by: umang2382
0 Replies

2. Shell Programming and Scripting

Need a script to see top processes for every hour

Hi All, I am new to Scripting , please give me guidance to write the script to see top processes on the Linux operating system. I executed this script on my Virtual Server(Linux) DATE=`date +%Y%m%d%H%M%S` HOME=/home/xmp/testing/xmp_report RADIUS_PID=`xms -xmp sh pr | grep... (2 Replies)
Discussion started by: madala
2 Replies

3. Shell Programming and Scripting

Warning in Top 10 cpu consuming processes

I m using following command to find top 10 cpu consuming processes. However whenever i execute the command i get following warning. What can be done to avoid it? # ps -auxf | sort -nr -k 3 | head -10 Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ root ... (6 Replies)
Discussion started by: pinga123
6 Replies

4. Solaris

top is showing 0% cpu Idle

What should we do if we show a 0% cpu idl on top? (5 Replies)
Discussion started by: Pouchie1
5 Replies

5. UNIX for Dummies Questions & Answers

How To Scroll Processes In top?

I'm using top to view processes. But, I do not know how to scroll down the list to view what is not showed in the terminal window. Anyone know how to do this? (1 Reply)
Discussion started by: keenansnews
1 Replies

6. AIX

Need a list of top 10 CPU using processes (also top 10 memory hogs, separately)

Okay, I am trying to come up with a multi-platform script to report top ten CPU and memory hog processes, which will be run by our enterprise monitoring application as an auto-action item when the CPU and Memory utilization gets reported as higher than a certain threshold I use top on other... (5 Replies)
Discussion started by: thenomad
5 Replies

7. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux use these values in shell script to kill processes exceeding cpu (pcpu) command does not give exact does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

8. UNIX for Advanced & Expert Users

top %CPU.

I am using the Red Hat Enterprise Linux AS release 3 (Taroon), 2.4.21-4.EL. When I see the output of the command 'top'. I am getting the following ************************************************************************************ 2 processes: 227 sleeping, 5 running, 0 zombie, 0 stopped... (3 Replies)
Discussion started by: praveen_b744
3 Replies

9. UNIX for Dummies Questions & Answers

Dual CPU's and 'top'

Hi have just built a new sunfire 280r with solaris 9 and i Have 2 questions 1) where can i view some information that will tell me for definate that the 2*900 mhz processors are both being used, i tried using "top" but it doesnt tell me for sure that both processors are churning away together ... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

10. UNIX for Dummies Questions & Answers

How Can I Have Top Display The Top 20 Processes??

how can i do that in a script withough havin the script halt at the section where the top command is located. am writign a script that will send me the out put of unx commands if the load average of a machine goes beyond the recommended number. top -n 20 i want to save this output to a file... (1 Reply)
Discussion started by: TRUEST
1 Replies
Login or Register to Ask a Question

Featured Tech Videos