[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Cron script to stop Apache when load is high

Hello,
 as I'm experiencing some unknown problem, which causes, that from time to time, the server just freezes, I need to fix this :

  1) during the day or night, the server is running fine. Then, for some reason the MySQL databaze is unable to handle all the queries (I did not identified yet WHY), and causes high load, memory is consumed and the server starts to swap, killing processes and usually get into some kind of unstable position.


  Until I will be able to fix this problem, I'd like to have a cron job, which will every minute check for the average load of the machine, and if the load is higher than let say 5.00, it issues this command : /etc/init.d/apache2 stop, waits for 10 minutes, and makes /etc/init.d/apache2 start

   Can anybody help me, how to do this script ?
   Thank you !
0
Letus
Asked:
Letus
  • 3
  • 3
  • 2
2 Solutions
 
LetusAuthor Commented:
PS. the machine is Gentoo Linux, Apache 2, mySQL 4.0
0
 
root_startCommented:
Hi Letus,

I created the following script and I think it can help you.

#!/bin/sh
##########################################################################

##########################################################################
# Global
##########################################################################
CommandToExecute="echo Test"
MaxLoadAverage="5"
SleepTime="600"
##########################################################################

##########################################################################
# Check Load Average
##########################################################################
checkLoadAvg ()
{      
      uptime_load_info=`uptime | cut -d":" -f4`
      one_minute=`echo ${uptime_load_info} | cut -d"," -f1`
      
      one_minute_tmp=`echo ${one_minute} \* 100 | bc | cut -f1 -d"."`
      max_load_tmp=`echo ${MaxLoadAverage} \* 100 | bc`
      
      if [ ${one_minute_tmp} -ge ${max_load_tmp} ]
      then
            echo 1
      else
            echo 0
      fi
}
##########################################################################

##########################################################################
# Exec Cmd
##########################################################################
execCmd ()
{
      cmd_complement="${1}"
      ${CommandToExecute} ${cmd_complement}
      if [ ${?} -ne 0 ]
      then
            echo "**ERROR: An error occurred while executing: [${CommandToExecute} ${cmd_complement}]."
            exit 1
      fi
}
##########################################################################

##########################################################################
# Main
##########################################################################
while true
do
      count_http=`ps -ef | grep http | grep -v grep | wc -l`
      if [ ${count_http} -ne 0 ]
      then
            if [ `checkLoadAvg` -ne 0 ]
            then
                  execCmd "stop"
                  sleep ${SleepTime}
            fi
      else
            execCmd "start"
            break
      fi
done
##########################################################################

Again, I hope it helps. =0)
0
 
root_startCommented:
Sorry, but I forgot to mention that the most important part of the script is the function "checkLoadAvg". =0)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LetusAuthor Commented:
Great, I will test it during weekend, but do I run this every minute as a cron job, or does that work as daemon ?
0
 
root_startCommented:
Yes... you run it from cron, and it will do the following:
If the load is higher than 5, it will execute the command specified in the variable "CommandToExecute" plus the word "stop", as a test I put in this variable the following command: "echo Test", but you must change it to: "/etc/init.d/apache2".

After stopping apache, it will sleep the time specified in the variable "SleepTime", and the value I put on it(600), means 10 minutes(10 * 60), after sleeping it will start apache again.

If you want it to run like a "daemon", just comment or remove the "break" and right after it, add "sleep 60", and it will sleep 1 minute. Do not forget to run the script, if executing like a "daemon", with nohup.

I hope it helps. =0)
0
 
ahoffmannCommented:
w|awk '(NR==1&&$(NF-2)>5.0){print "/etc/init.d/apache2 stop;sleep 600;/etc/init.d/apache2 start"}'|sh
0
 
LetusAuthor Commented:
thank you, I will test this too ...

is there any chance, that this scripts echo something into the system log ? (var/log/messages) ? so I know that it was doing something, when I'm away from the server ?
0
 
ahoffmannCommented:
> is there any chance, that this scripts echo something into the system log  ..
if you have /bin/logger, then you may use something like:

w|awk '(NR==1&&$(NF-2)>5.0){print "/etc/init.d/apache2 stop;/bin/logger high-load;sleep 600;/etc/init.d/apache2 start"}'|sh
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now