individual cpu % in 'top' output

10

2

Usually to view individual cpu % we press '1' in the interactive mode of top.

However I am not able to figure out the logic to get the same output in batch mode i.e,

top -n1b

I am redirecting this output to a file to view it later and stuff like that, so I need the batch mode. Is it possible? Installing a separate tool for this is not gonna be possible.

I'm looking for an option that I am missing or some way to capture the output.

Uday

Posted 2010-01-22T08:57:05.877

Reputation: 215

Answers

7

In interactive top, switch to individual CPU % by typing 1, then write the current configuration to a file by typing W. This will create ~/.toprc. Now, top -n1b will output per-CPU % as well. You may need to copy the .toprc file and change permissions if your script runs as a different user.

jwhitlock

Posted 2010-01-22T08:57:05.877

Reputation: 620

1The Q asks for batch mode. – Charles Stewart – 2010-01-25T09:49:59.370

2I'll try to be clearer: I know of no way on the command line to tell top "when you are in batch mode, print the individual cpu %". However, top does read .toprc in batch mode, so you can setup that file as I described, and then you'll get individual cpu % in batch mode as well. Or, at least it did for me (procps version 3.2.7) – jwhitlock – 2010-01-26T23:15:39.540

does it have to be top ? Why not use ps and sort on %CPU? I find %CPU misleading even, I tend to sort on VSIZE or RSS, because often what knocks my servers down are rogue memory hogs. – memnoch_proxy – 2010-01-31T06:04:20.290

5

Does it have to be top? Maybe you need mpstat

  mpstat -A

Dan Andreatta

Posted 2010-01-22T08:57:05.877

Reputation: 211

0

You can use atop. Its a very comprehensive monitor, where you can monitor processes as well as system resources such as memory and CPU. It gives individual output for each CPU core.

For ubuntu systems, its available as a package in the repo, so you can directly install it via apt-get/synaptic.

For Redhat and derived systems, you can find the packages here : http://dag.wieers.com/rpm/packages/atop/

s1d

Posted 2010-01-22T08:57:05.877

Reputation: 296

0

This is sort of a hack but seems to work:

$ ssh local.ubuntu.server "(sleep 1;echo 1)|TERM=xterm script -c top & sleep 2;killall script"|grep '^%Cpu'
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.1 st
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

AXE Labs

Posted 2010-01-22T08:57:05.877

Reputation: 509

0

I don't know the right way to do this with the procutils top; maybe echo k|top -n2 -d1.0 does the trick? Note that the first output of top isn't going to give you useful info on cpu% usage: top only attempts to estimate cpu% since it started running. Hence the -n2. You can restrict to the second statistics listing by throwing away everything up to the second blank line.

With the BSD top, top -l2 -s1 -o cpu will (tested) print out the top statistics twice, order by cpu%. Just in case there is a BSD top lurking about on your system...

Charles Stewart

Posted 2010-01-22T08:57:05.877

Reputation: 2 624