You are not logged in.

#1 2007-12-28 20:53:42

skorpan
Member
Registered: 2007-11-09
Posts: 81

CPU usage in shell scripting

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

#2 2007-12-28 21:02:35

nDray
Member
From: Portugal
Registered: 2007-01-21
Posts: 143
Website

Re: CPU usage in shell scripting

Offline

#3 2007-12-28 21:29:55

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: CPU usage in shell scripting

That also helps me nothing.

Offline

#4 2007-12-28 22:08:46

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: CPU usage in shell scripting

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

#5 2007-12-28 23:14:36

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: CPU usage in shell scripting

Gilneas wrote:

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

#6 2007-12-29 03:40:22

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: CPU usage in shell scripting

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

#7 2007-12-29 03:53:58

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: CPU usage in shell scripting

skorpan wrote:

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

#8 2007-12-29 15:08:09

delphiki
Member
Registered: 2007-11-17
Posts: 66
Website

Re: CPU usage in shell scripting

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

#9 2007-12-30 17:38:44

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: CPU usage in shell scripting

Thanks to everyone who replied. I just want to add that this is not at all homework, I'm just configuring dzen2.

Offline

Board footer

Powered by FluxBB