12

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.

5 Answers 5

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.

3
  • 2
    The Q asks for batch mode. Jan 25, 2010 at 9:49
  • 2
    I'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
    Jan 26, 2010 at 23:15
  • 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. Jan 31, 2010 at 6:04
5

Does it have to be top? Maybe you need mpstat

  mpstat -A
1

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
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...

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/

You must log in to answer this question.

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