System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

running 'top' command in batch mode

 
SOLVED
Go to solution
Shabu Khan-2
Frequent Advisor

running 'top' command in batch mode

I didn't get an answer in the linux section of this forum, so posting it here ... anyway know the answer to this?
-----------
I am trying to run the top command in batch mode and would like to sort that on either the RES field or VIRT field, how do I do that?

What is the switch to sort any field in batch mode?
# top -b -n 1
4 REPLIES

Re: running 'top' command in batch mode

>I didn't get an answer in the linux section

HP-UX is different. There is no -b.

>would like to sort that on either the RES field or VIRT field, how do I do that?

You'll have to strip off the header and use sort -k.
Perhaps it would be using to use ps(1) and sort on its fields?
James R. Ferguson
Acclaimed Contributor
Solution

Re: running 'top' command in batch mode

Hi:

Even looking at the GNU/Linux 'top' command manpages indicates to me that you need to roll-your-own if you are running in batch mode. You might do something along these lines:

# top -b -n 1|awk '{if ($1~/[0-9]+/) {if ($5~/m/) {$5=int($5*1024*1024)};print}}'|sort -k5rn,5

This stripes the header liness (since they don't have a numeric first field. If the VIRT field units are "m"egabytes, then this field is scaled to match the other values in this column. You might need additional code for other scaling. The output is then piped to 'sort' and shown in descending size order.

Regards!

...JRF...


Shabu Khan-2
Frequent Advisor

Re: running 'top' command in batch mode

Thanks JRF.
Thats exactly what I need.

Thanks for the responses guys.

Thanks,
Shabu
MarkSeger
Frequent Advisor

Re: running 'top' command in batch mode

Another alternative is to run collectl - think sar on steroids. It will collectl just about everything about you system, including process data, which you can then play back later.

It has a --top switch which will play back process data in a format similar to 'top' which by default will show the top 10 consumers of CPU for each sample period which is a minute. You can even tell it to sort of things like memory, I/O, etc and can change the number of processes reported and/or the sample period at collection time.

-mark