Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I execute following command to get cpu usage , I get nice + user cpu usage.

top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}' 

Output:

14.5

Here I am getting problem is that the output depends on top command thus it doesn't change instantly as top command. So I am not getting correct cpu instantly. It gives same output and not changing.

I want to get realtime cpuusage in output. Please help me to improve my command.

share|improve this question
1  
And the question is? –  Dennis Kaarsemaker Mar 26 '13 at 9:29
    
Try running the command prepended with time. On my system it only takes 0.165s. –  depquid Mar 26 '13 at 18:16
    
Please give me edited command. –  K.K Patel Mar 28 '13 at 6:39
    
Try: time (top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}' ) –  depquid Mar 28 '13 at 20:59
    
no diffrence in output,after tested in different systems –  K.K Patel Mar 29 '13 at 4:43
show 2 more comments

6 Answers 6

You're right, top appears to give incorrect CPU usage on first iteration. You can work around this issue like this:

top -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4}'

This will of course take twice as much time, but it will work anyway.

If you still want it work faster, you can use -d option, e.g. for 1-second total period of measurement use half of it:

top -d 0.5 -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4}'
share|improve this answer
add comment

Just to expand on @Ruslan's answer, 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:

top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'
share|improve this answer
add comment

Use watch command or for loop in shell,

i.e watch -n1 "top -b -n1 | grep 'Cpu(s)' | awk '{print \$2 + \$4}'"

share|improve this answer
    
Same thing Happens with watch –  K.K Patel Mar 26 '13 at 9:49
add comment

Maybe use this one based on ps output :

ps aux  | awk 'BEGIN { sum = 0 }  { sum += $3 }; END { print sum }
share|improve this answer
add comment

Why do you care about the CPU usage? The load average is a much better indicator:

cat /proc/loadavg

If you really need the CPU usage, parsing /proc/stat is probably the way to go.

share|improve this answer
    
Load average is basically a time integral of CPU usage (divided by time). Why would you say an integral characteristic is much better indicator [of everything] than a differential one when they have completely different use cases? Also, /proc/stat doesn't give CPU usage as it is, one has to read it twice with some time between reads and do some calculations to get CPU usage. This is just what top does. –  Ruslan Sep 17 '13 at 11:41
    
I can't math, so I have to admit I don't really understand the difference between integral and differential :/ "Load average is basically a time integral of CPU usage" - is it really? AFAIK a process that hangs in the D state causes a load increase, but no CPU usage. "Why would you say..." - OP doesn't say what he really wants to accomplish, so to be honest, I just assume he wants to measure his system load and thought that the CPU usage would be the best indicator for this, so I directed him to load average. –  Martin von Wittich Sep 17 '13 at 14:13
    
From your link: "Systems calculate the load average as the exponentially damped/weighted moving average of the load number." and "the system load is a measure of the amount of computational work". D state isn't taking any computational work. What is taking is the processes which use CPU time. So average load is basically percentage of time CPU wasn't idle for some period of time (say, minutes to tens of minutes). And CPU usage is percentage of time CPU isn't idle at time scale of milliseconds - i.e. updated every time you read it and reflecting current state. –  Ruslan Sep 17 '13 at 14:22
add comment

Using sar command

sar 1 10

That means: get 10 times result in every second

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.