You are not logged in.
Pages: 1
Hello. I've been looking around for a solution to this seemingly simple problem for a few hours now, without success. I need a shell script which returns the CPU usage of my two CPU cores.
Answers to your questions:
* YES, it must be a shell script.
* Replying with "cat /proc/stat" helps me NOTHING, as I have no idea how to read the CPU usage from those silly numbers.
Last edited by skorpan (2007-12-28 20:54:01)
Offline
Offline
That also helps me nothing.
Offline
If it's percentages you want you could extract the data from top.
Like this for example:
top -b -n 1 | sed -ne '/Cpu/ s/.* \([0-9]*\.[0-9]*\)%us.* \([0-9]*\.[0-9]*\)%sy.*/User: \1%, System: \2%/p'
(output)
User: 10.1%, System: 0.8%
Offline
If it's percentages you want you could extract the data from top.
Like this for example:top -b -n 1 | sed -ne '/Cpu/ s/.* \([0-9]*\.[0-9]*\)%us.* \([0-9]*\.[0-9]*\)%sy.*/User: \1%, System: \2%/p' (output) User: 10.1%, System: 0.8%
Thanks a lot. The script is pretty slow though (or rather "top" is slow), is there any faster idea out there?
Offline
Sounds sort of like homework to me, but this should help a little.
uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/" -e "s/ //g"
ALSO:
cat /proc/loadavg
Shows you the one minute, five minute, and fifteen minute load average
Last edited by crouse (2007-12-29 03:42:11)
Offline
The script is pretty slow though (or rather "top" is slow), is there any faster idea out there?
The way I see it is, /proc/stat gives your mileage and top polls it twice with a time delay and calculates the speed (and compares with the processor speed for the percentage).
So it's not that top is slow, it just has to wait or it won't be able to give an accurate result.
Offline
This one is sorta like the one crouse posted, but I find that it works fast enough for me:
#!/bin/sh
while true
do
echo -n $(uptime | sed 's/.*://; s/,//g')
sleep 1
done
That code should give you the load average and update itself every second (approximately)
Hope that helps a little bit...
EDIT: this only gives you the combined load average for two or more cpus...
Last edited by delphiki (2007-12-29 15:27:15)
Offline
Thanks to everyone who replied. I just want to add that this is not at all homework, I'm just configuring dzen2.
Offline
Pages: 1