Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
The Q asks for batch mode. –  Charles Stewart Jan 25 '10 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 '10 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. –  memnoch_proxy Jan 31 '10 at 6:04
add comment

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

share|improve this answer
add comment

Does it have to be top? Maybe you need mpstat

  mpstat -A
share|improve this answer
add comment

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/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.