Hi,
I'm new to shell scripting. I need to make a script to add on to my cronjobs.


The script must get the value of load average from my server and if its greater than 10 it should stop my apache service. I cant find a way to get the value of load average in integer type to do the check. Any help???

Regard

why only 10? and why would you want to restart Apache based on load avg?

Load average is a calculation loosely based on the amount of time a process has to wait for the CPU, I have personally seen my BSD boxes hit an excess of 600 load average before choking. You should be looking at % idle not the load average. All of this data is available to you via the /proc filesystem(on Linux).

A better measurement would be to use either Apache (remotely if course) or count the amount of processes that are happening. You can also use a monitoring service or program to "ping" the server at a predetermined interval, or grab a web page, and notify you based on response time. or even better, fix whatever it is that is causing you to have these runaway processes.

It has been my experience that it is never good to blindly restart processes like this. What happens if there is a syntax error in one of the config file? or maybe some permissions changed on a logfile and Apache cant get a lock on it?

I'm actually not trying to stop the webserver. There is a page to be loaded id the load average is high on another server. i'm able to get the value from an "uptime" command. but the value i get is in a string format. So when i try comparing the value

uptime|gawk '{ print $12 }' > load

while read Inbr
do
echo $Inbr
if [ $Inbr -gt 77 ]; then
echo "Greater than 77"
else
echo "Less or equal to 77"
fi
done < load

I get an error
0.04
./test: line 7: [: 0.04: integer expression expected
Less or equal to 77

How can i change the value to integer type?????

How about this?

[I]uptime|gawk  '{ print $12 }' | tr -d '.'   > load
while read Inbr
  do
    echo $Inbr
    if [ `bc -l <<< $Inbr` -gt 77 ]; then
      echo "Greater than 77"
      else
         echo "Less or equal to 77"
      fi
done < load[/I]

Thanks!!! It worked fine. I need a minor help now. I need to store a falg value to a file. I do it by redirecting the value. But how can i retrieve the value back from the file to a variable?? For eg: I have a file "flag" which has the value "0" in it. How do i pass the value to a variable in the script??

Thanks!!! It worked fine. I need a minor help now. I need to store a falg value to a file. I do it by redirecting the value. But how can i retrieve the value back from the file to a variable?? For eg: I have a file "flag" which has the value "0" in it. How do i pass the value to a variable in the script??

use cat

Thanks!!! It worked fine. I need a minor help now. I need to store a falg value to a file. I do it by redirecting the value. But how can i retrieve the value back from the file to a variable?? For eg: I have a file "flag" which has the value "0" in it. How do i pass the value to a variable in the script??

var=`cat file`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.