individual cpu % in 'top' output

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.


Solution 1:

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.

Solution 2:

Does it have to be top? Maybe you need mpstat

  mpstat -A