Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
09-25-2005, 01:12 AM
#1
Member
Registered: Aug 2003
Posts: 50
Rep:
find remaining percentage of cpu
Hi guys,
I'm trying to find out the remaining percentage of cpu. How do I do that?
I just want the cpu's status, and nothing else
I have tried vmstat, free. But that didn't give me the result I want.
09-25-2005, 02:45 AM
#2
Member
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332
Rep:
Do you mean the CPU idle time? Try top , the line beginning with CPU, the entry named "id".
09-25-2005, 04:10 AM
#3
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165
Can you elaborate on why vmstat doesn't give you the result you want ?
09-26-2005, 03:50 PM
#4
Member
Registered: Aug 2003
Posts: 50
Original Poster
Rep:
Thank you for responding.
vmstat just return this:
procs memory swap io system cpu
r b swpd free buff cache si so bi bo in cs us sy wa id
1 0 35000 1644948 411968 1127824 0 0 1 1 0 0 0 0 0 0
I know top can retrieve cpu's information, but it also returns other stuffs like:
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND
I just want to %CPU. Is there any combination of command to get the %CPU?
09-26-2005, 04:39 PM
#5
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
when I do vmstat I get this output
Code:
shane@mainbox ~ $ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 208000 16904 128524 0 0 4 3 1104 899 10 5 85 0
the above code is kind of missallighned. I think idle is 85 or 85%
this code will get it
Code:
my_array=($(vmstat)
echo ${my_array[36]}
I am not sure why your cpu usage according to vmstat is 0
Last edited by shanenin; 09-26-2005 at 04:41 PM .
09-26-2005, 05:03 PM
#6
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
I was wasting all kinds of time trying to put the output of top inot one big string then parsing the info. You can do it with the same technicqe I used above
Code:
my_array=($(top -n 1))
echo ${my_array[30]}
09-26-2005, 05:45 PM
#7
Member
Registered: Aug 2003
Posts: 50
Original Poster
Rep:
I followed your suggestion and keep getting 0
procs memory swap io system cpu
r b swpd free buff cache si so bi bo in cs us sy wa id
0 0 35000 1833020 411376 1156680 0 0 1 1 0 0 0 0 0 0
at position 36, it is 0. It's correct, but I dont' know if that is the value of cpu's idel.
09-26-2005, 05:49 PM
#8
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
did you try my same method(above) using top, that should work for you.
09-26-2005, 05:54 PM
#9
Member
Registered: Aug 2003
Posts: 50
Original Poster
Rep:
thank you for a very quick reply.
I just followed the top example, but it returned this: irq
09-26-2005, 06:01 PM
#10
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
look when I enter this command
Code:
mainbox shane # top -n 1
top - 17:55:29 up 10:00, 3 users, load average: 0.24, 0.18, 0.09
Tasks: 61 total, 1 running, 60 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.3% us, 4.5% sy, 4.9% ni, 85.9% id, 0.1% wa, 0.2% hi, 0.1% si
Mem: 514760k total, 273636k used, 241124k free, 19896k buffers
Swap: 506036k total, 0k used, 506036k free, 133936k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10091 shane 15 0 60972 32m 15m S 2.0 6.4 3:24.41 python
1 root 16 0 1468 500 436 S 0.0 0.1 0:00.35 init
**** I cut some off****
Code:
mainbox shane # my_array=($(top -n 1))
mainbox shane # echo ${my_array[30]}
85.9%
If I count from left to right(spaces seperate my elements) starting with 0, the 85.9% is the 30th element. what does your top -n 1 look like?
Last edited by shanenin; 09-26-2005 at 06:02 PM .
09-26-2005, 07:32 PM
#11
Member
Registered: Aug 2003
Posts: 50
Original Poster
Rep:
yes, that's what I'm looking for...thank you very much for your help...
09-26-2005, 08:02 PM
#12
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
your welcome :-)
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 04:30 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News