Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by xrserver View Post
    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
    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.

    Code:
    awk -F "." '{print $1}' /proc/loadavg
    There's no need to use awk in bash, though.

    Code:
    var=$(< /proc/loadavg)
    var=${var/.*/}
    Also, you assume that each iteration in the while loop will last 1 second. That's not true. Read about the sleep command.

    Code:
    sleep 200
    Would suffice.

  3. #3
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...