Results 1 to 3 of 3
Ok well I am very new to shell scripting. I have a script which I want to check the server load and if it is above a certain number it ...
- 02-29-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 1
Help with Shell Script
Ok well I am very new to shell scripting. I have a script which I want to check the server load and if it is above a certain number it should wait X number of seconds before continuing. Right now this all thats in the script but I have another script I wrote which i will be putting this in.
I keep getting this error and as I said I am very new so I do not know really how to fix it.
line 4: [: 0.20: integer expression expected
This is my script so far
#!/bin/bash
load=`awk '{print $1}' /proc/loadavg`
echo $load
if [ $load -gt 1 ]
then
cool='200'
echo "Server load is above 1, cooling down"
time='0'
while [ $time != $cool ]
do
time=`expr $time + 1`
done
else
echo "Server Load is $load no need to cool down"
fi
Any help will be greatly appreciated.
- 03-01-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The load average is expresed as a number with decimal cyphers. Bash built in arithmetics can only handle integers. If you only care about the integer part, you can just discard the rest.
There's no need to use awk in bash, though.Code:awk -F "." '{print $1}' /proc/loadavg
Also, you assume that each iteration in the while loop will last 1 second. That's not true. Read about the sleep command.Code:var=$(< /proc/loadavg) var=${var/.*/}
Would suffice.Code:sleep 200
- 03-01-2008 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
try this construct instead. you use the math inside awk to do the number comparison
Code:load=$(awk '$1 > 1 { print "overload" }' "/proc/loadavg") if [ $load == "overload" ];then echo "cooling down" sleep 200 # code fi


Reply With Quote
