0

I'm trying to slow down my infinite loop if CPU load exceeds certain limit, but, its just not working out right, below is the code. The if condition always results true

c=1
while [ $c -le 1 ]
do
#echo "Welcome $c times"
#php BALHABLH.php

IN=$(cat /proc/loadavg);

set -- "$IN"
IFS=" "; declare -a Array=($*)
echo "${Array[@]}"
echo "${Array[0]}"
echo "${Array[1]}"

#var = ${Array[1]}



x=$(expr "${Array[1]}" )

if [ $x > 0.91 ]
then
    echo "CPU LOAD > 0.91"
    sleep 2
fi


(( c++ ))
done
4
  • Thought about using nice and delegate load handling to your scheduler? Commented Mar 1, 2013 at 16:12
  • > is a redirection operator. You want -gt. Commented Mar 1, 2013 at 16:16
  • 2
    @n.m. Unless this is ksh93, floating point arithmetics would not work.. Commented Mar 1, 2013 at 16:24
  • I got Centos 64 bit 6.something. I wonder if we can multiply with 100, then convert to integer? I'm really poor in bash, noob in php, therefore seek help here. THanks to ya all for the help! Commented Mar 1, 2013 at 16:49

3 Answers 3

2

You need to use bc for floating point comparison and use (( ... )) for arithmetic expressions:

if (( $(bc -l <<< "$x > 0.91") == 1 ))

Also don't use cat, use:

IN=$(</proc/loadavg)
Sign up to request clarification or add additional context in comments.

1 Comment

dogbane, tried your tip, now I'm getting this error: line 21: ((: == 1 : syntax error: operand expected (error token is "== 1 ")
0

Bash cannot use floating point arithmetic. You could do something like this:

if [ $( echo "$x > 0.91" | bc ) -eq 1 ]; then

1 Comment

Thank you so much people. with your help, the code is working now!
0

Bash only handles integers. To handle floats pipe to bc like this:

[ $(echo " $x > 0.91" | bc -l) -eq 1 ]

bc returns 1 if the comparison is true. We compare with 1 (using the -eq operator).

Validation

$ cat test.sh
#!/bin/bash  
x="$1"
if [ $(echo " $x > 0.91" | bc -l) -eq 1 ]; then
   echo greater;
else 
   echo smaller;
fi
$ ./test.sh 0.5
smaller
$ ./test.sh 1.5
greater

You can also simplify your script a bit like this:

#!/bin/bash
c=10
for (( i=1;i<=c;i++ )); do
    load=$(awk '{print $2}' /proc/loadavg)
    echo "$i: load is $load"
    if (( $(echo "$load > 0.91" | bc) == 1 )); then
        echo "CPU LOAD > 0.91"       
        sleep 2
    fi
done

6 Comments

Dear user000001, After implementing your advise, now I'm getting this error: "integer expression expected"
After this fix: "[ $(echo " $x > 0.91" | bc -l) -eq 1 ]" I'm now getting this error: ./loop1.sh: line 21: bc: command not found ./loop1.sh: line 21: 0.51 > 0.91: command not found ./loop1.sh: line 21: [: -eq: unary operator expected
do you have bc on your system? try echo " 1+1" | bc from a console
you right mate, the problem is here: echo " 1+1" | bc -bash: bc: command not found Should I fire yum install bc now"
@user17183 Yes install bc and you will be fine. I also simplified your script a bit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.