1

I am trying to find an alternative to this command:

mpstat 5 1

Even though it works as i need, but it have a delay of 5 seconds, i need something similar, but that will give me instantly the CPU usage average of last 5 seconds.

  • Does it need to be the previous 5 seconds from when you run the command? There's nothing that stores that information, but you can run a background command (like mpstat) that records and stores a 5 second average, and than have another command show that info. – Brian Oct 9 '15 at 12:13
  • You can look at the sar command but that usually only polls every ten minutes. – Bratchley Oct 9 '15 at 14:39
  • @Brian any suggestion of efficient way to store and reuse that info ? – sharp12345 Oct 10 '15 at 9:46
3

Use a butt-simple script to record it:

while true; do
     mpstat 5 1 > tmpstat
     cp tmpstat laststat
done

Then run the above script in the background. The last mpstat will be available immediatley in the file laststat. It will be, however, be from 0-5 seconds stale.

2

You can use vmstat

rbabchis@haze:~$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 333020 308684 1609404    0    0    20    21    5    9  2  2 96  0  0
 0  0      0 332392 308692 1609560    0    0    26    38 1655 4276  4  1 95  0  0
 0  0      0 332228 308692 1609588    0    0     6    30 1731 4381  4  2 95  0  0

It looks like you need "vmstat 5 2" to give you an average just once. I don't think the first line counts.

rbabchis@haze:~$ vmstat 5 2 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 315632 309116 1615048    0    0    20    21    5   10  2  2 96  0  0
 0  0      0 335676 309128 1615056    0    0     0    54 1861 5291  3  3 94  0  0
  • vmstat 5 2 also have 5 seconds delay, the same as mpstat 5 1 – sharp12345 Oct 10 '15 at 7:58
  • Yeah woops... you'll need something recording constantly to get that info when needed. – Ryan Babchishin Oct 12 '15 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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