Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We know that top is a command to get the CPU usage in Linux and other ways include cat /proc/stat which is used by top.

What are other methods to get the CPU usage in the Linux command line?

Use case scenario: The output of this command will be used by a script to log the CPU usage continuously. top and /proc/stat give detailed usage which is difficult to log. Is there an easier way to get the CPU info?

share|improve this question

closed as primarily opinion-based by bwDraco, soandos, Breakthrough, gronostaj, Nifle Jun 26 '13 at 15:12

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

what is wrong with the ones you quote? – pataluc Jun 20 '13 at 11:18
@pataluc It give more detailed information. I need a overall CPU usage say CPU used : 91% Mem Used:78% Mem Free:22% something like this.. – fasil Jun 20 '13 at 12:54
see Nathan's answer... you can sed the result if you only want the value. – pataluc Jun 20 '13 at 12:56
Voting to close as "not constructive". From "To prevent your question from being flagged and possibly removed, avoid asking subjective questions where … your answer is provided along with the question, and you expect more answers: 'I use ______ for ______, what do you use?'" – bwDraco Jun 24 '13 at 21:15
@DragonLord actually, there are quite limited ways of doing this and the two answers here are in fact useful. Mine points out a very common misconception about top and the other gives a very useful command. It is not a I use X, how 'bout you? question, but a How do I do X question. Voting to reopen. – terdon Dec 11 '13 at 14:23
up vote 9 down vote accepted

As others have said, the best way is probably top. It needs a little tweaking and a little parsing but you can get it to give you the current CPU use as a percentage.

top splits CPU usage between user, system processes and nice processes, we want the sum of the three. So, we can run top in batch mode which allows us to parse its output. However, as explained here, the 1st iteration of top -b returns the percentages since boot, we therefore need at least two iterations (-n 2) to get the current percentage. To speed things up, you can set the delay between iterations to 0.01. Finally, you grep the line containing the CPU percentages and then use gawk to sum user, system and nice processes:

    top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'
        -----  ------   -----------    ---------   ----------------------
          |      |           |             |             |------> add the values
          |      |           |             |--> keep only the 2nd iteration
          |      |           |----------------> keep only the CPU use lines
          |      |----------------------------> set the delay between runs
          |-----------------------------------> run twice in batch mode

I thought you could also get this information through ps -o pcpu ax by adding the %use of each running process. Unfortunately, as explained here, ps "returns the percentage of time spent running during the entire lifetime of a process" which is not what you need.


Based on your comment, your version of top is different to mine and you should use this instead:

top -bn 2 -d 0.01 | grep '^Cpu.s.' | tail -n 1 | gawk '{print $2+$4+$6}'
share|improve this answer
I deleted my answer because what you said is correct. This one seems to be a valid method. – Nathan C Jun 20 '13 at 17:32
@NathanC I thought your answer was correct too, it was what I would have answered. Then I tested it and was surprised :). – terdon Jun 20 '13 at 17:34
@terdon Thanks for your detailed answer... I couldn't able to get the output for the above command. Is there any syntactical modification required.. – fasil Jun 21 '13 at 3:53
@fasil no, it should work. What is the problem? Any error messages? – terdon Jun 21 '13 at 3:58
@terdon I don't get any output for the complete command.. with #top -bn 2 -d 0.01 I get the full result but on adding grep '^%Cpu' I don't see any output just the prompts returns no error... – fasil Jun 21 '13 at 5:54

sar is the definitive way to do it. So for instance sar -u will output something like this:

08:30:01 AM       CPU     %user     %nice   %system   %iowait     %idle
08:40:01 AM       all      6.94      0.00      1.77      4.92     86.36
08:50:01 AM       all      5.73      0.00      2.31     12.72     79.24
09:00:01 AM       all      5.95      0.00      2.58     18.36     73.11
09:10:01 AM       all      6.88      0.00      2.22     17.44     73.45
09:20:01 AM       all      8.61      0.00      2.68     27.93     60.78

You don't say which Linux you use but for CentOS/RedHat you need to install the sysstat package, and I think it's the same on Debian/Ubuntu.

You can also use sar to gather statistics ad hoc:

sar -o /tmp/sar.out 60 600

Will gather stats every 60 seconds for 600 minutes.

share|improve this answer

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