0

I am using java to run a command on linux. Since top is an interactive command and need to press 1 to get the info of all the cpu(s) so I cannot use that. So I am using the following command to get the cpu(s) info:

cat /proc/stat|grep "^cpu[0-9]* ";sleep 3;cat /proc/stat|grep "^cpu[0-9]* "

I am getting the output as :

cpu  4673683 193 832132 1544221346 142352 1220 171760 0 0
cpu0 2473973 90 524817 769734476 73628 1124 158588 0 0
cpu1 2199709 103 307315 774486870 68723 95 13171 0 0
cpu  4673683 193 832133 1544221744 142352 1220 171760 0 0
cpu0 2473974 90 524817 769734674 73628 1124 158588 0 0
cpu1 2199709 103 307315 774487069 68723 95 13171 0 0

Now my problem is how to get the percentage out of it as top command is showing.

 Cpu0  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
1

Try the mpstat utility from the sysstat package:

mpstat -u -P ALL

Output on CentOS host (virtual host with 1 virtual cpu):

Linux 3.10.0-229.4.2.el7.x86_64 (testhost.testdomain)   08/11/2015  _x86_64_    (1 CPU)

10:03:57 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
10:03:57 AM  all    0.21    0.05    0.29    0.04    0.00    0.13    0.00    0.00    0.00   99.28
10:03:57 AM    0    0.21    0.05    0.29    0.04    0.00    0.13    0.00    0.00    0.00   99.28

Output on Ubuntu host:

Linux 3.13.0-24-generic (myhost)    11-08-15    _x86_64_    (4 CPU)

10:05:37     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
10:05:37     all    3,34    0,04    1,16    0,13    0,08    0,00    0,00    0,00    0,00   95,25
10:05:37       0    2,81    0,06    1,93    0,13    0,11    0,00    0,00    0,00    0,00   94,96
10:05:37       1    3,94    0,03    0,78    0,10    0,03    0,00    0,00    0,00    0,00   95,13
10:05:37       2    3,31    0,02    1,21    0,16    0,07    0,00    0,00    0,00    0,00   95,24
10:05:37       3    3,31    0,05    0,73    0,14    0,10    0,00    0,00    0,00    0,00   95,68
| improve this answer | |
  • I cannot install new package mpstat it is not provided by default. I am using Centos – user1188867 Aug 11 '15 at 8:11
0

May be try like this:

 top -b -n 1 | grep -i "^cpu"

Following is output on my machine:

wbzz@mambet:~$ top -b -n 1 | grep -i "^cpu"
Cpu0  : 10.5%us,  4.5%sy,  0.0%ni, 82.4%id,  1.6%wa,  0.0%hi,  1.0%si,  0.0%st
Cpu1  : 44.5%us, 15.0%sy,  0.0%ni, 40.2%id,  0.2%wa,  0.0%hi,  0.2%si,  0.0%st
Cpu2  : 43.8%us, 18.1%sy,  0.0%ni, 37.7%id,  0.1%wa,  0.0%hi,  0.2%si,  0.0%st
Cpu3  : 45.1%us, 14.5%sy,  0.0%ni, 40.3%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st

This works on slackware-current. On CentOS machine need to change grep expression to:

top -b -n 1 | grep -i "^%cpu"
| improve this answer | |
  • Could you please relate and explain how your answer is solving the OP ? – neuron Aug 11 '15 at 7:17
  • By default when you run top program it runs interactively. But you can run it in batch mode (using -b option). And then parse required information. So you will get same result like running top program. – Murad Tagirov Aug 11 '15 at 7:24
  • Have you tried it & got any successful results ? – neuron Aug 11 '15 at 7:25
  • Yes. Result on my machine like following: Cpu(s): 25.2%us, 9.5%sy, 0.0%ni, 63.7%id, 1.0%wa, 0.0%hi, 0.6%si, 0.0%st – Murad Tagirov Aug 11 '15 at 7:26
  • When you are doing a grep -i "^cpu" , it doesn't start with cpu, it starts with %Cpu(s) – neuron Aug 11 '15 at 7:29

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.