Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check cpu process to running multiple scripts 1

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
0
0
AU
Hi guys,

I'm trying to do dynamic multiple loading data,
because the data size is difference, if the data is big the loading process takes a while and taking much cpu,
so I want to load data pararelly as many as I can before reach 350% cpu because we have 4 cpus (400%).
just wondering whether there is a way of doing this in pseudo code :


say I have 20 data
Code:
for (i=1;i<=20;i++)
do
    while [cpu process -lt 90%]
    do
         loading data[i] &
    done
done
wait

I'm stuck in checking cpu process, please show some ideas...

Thanks guys.
 
How about something like...
Code:
#!/bin/ksh

vmstat 1 2 | tail -1 | sed 's/^.* //g' | read IDLE

(( CPU = 100 - IDLE ))

print "CPU Usage = ${CPU}"
print "CPU Idle  = ${IDLE}"
Your pseudocode has some errors. That inner loop shouldn't be a "while". You could end up running "loading data" many times without incrimenting "i".

This might be better...
Code:
for (i=1;i<=20;i++)
do
    while [cpu process -gt 90%]
    do
         sleep 60
    done

    loading data[i] &
done
wait
 
Sambones,

r u sure the idle is the cpu idle not the "cpu usage"?
and ur code doesn't work,
why can't they parse the IDLE parameter?
is that something wrong with read command?

can u explain "vmstat 1 2" means?

Cheers,
 
man vmstat

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Are you sure you're using the Korn shell (/bin/ksh)? This code is Korn shell code and probably won't work in other shells.

That line starting with the "[tt]vmstat[/tt]" is to get the CPU usage. The parameters "1 2" mean give us two lines, one second apart. This is because the first line is a total from when the system started, and the second line will be the current vmstat values.

The "[tt]tail[/tt]" is to just get us that last line. The "[tt]sed[/tt]" is to get just the last field, which is the current CPU idle time. This line should work in any shell.

The next line, with the double parenthesis just subtracts the idle time from 100 to get the CPU usage (since it's a percentage). This line will only work in the Korn shell.

The following "[tt]print[/tt]" commands are also Korn shell only.

And, PHV is correct, the "[tt]man[/tt]" pages can answer most questions. And a lot quicker than you can get a response here.
 
Hi

SamBones said:
The next line, with the double parenthesis just subtracts the idle time from 100 to get the CPU usage (since it's a percentage). This line will only work in the Korn shell.
That mathematical evaluation work in [tt]bash[/tt] too. Only the [tt]print[/tt] builtin function is [tt]ksh[/tt] only.

Anyway, your code will not work because the pipe determines the execution of [tt]read[/tt] in a subprocess.
Code:
[blue]master #[/blue] vmstat 1 2 | tail -1 | sed 's/^.* //g'
99

[blue]master #[/blue] vmstat 1 2 | tail -1 | sed 's/^.* //g' | read IDLE; echo "-$IDLE-"
--

[blue]master #[/blue] vmstat 1 2 | tail -1 | sed 's/^.* //g' | ( read IDLE; echo "-$IDLE-"; )
-99-


Feherke.
 
Thanks feherke,

YOu got the point.
 
[sad] well, it works on Solaris' KSH.

Code:
$ uname -a
SunOS cable 5.10 Generic_118833-17 sun4u sparc SUNW,Ultra-2
$ vmstat 1 2 | tail -1 | sed 's/^.* //g' | read IDLE; echo "-$IDLE-"
-97-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top