#!/bin/bash
# script to check server for extremely high load and restart Apache if the condition is matched
# Written by hip0
# http://www.pc-freak.net
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
# define max load avarage when script is triggered
max_load='25'
# log file
high_load_log='/var/log/apache_high_load_restart.log';
# location of inidex.php to overwrite with temporary message ( note that the file has to exist I haven't added because it sacrifces performance)
index_php_loc='/home/site/www/index.php';
# location to Apache init script
apache_init='/usr/local/etc/rc.d/apache2';
#
site_maintenance_msg='
Site maintenance, please excuse usSite Maintenance in progress - We will be back online in a minute
';
# temporary pid file
pid='/tmp/apache_restart.pid';
cp -rpf $index_php_loc.bak_ap index_php_loc.bak_ap.orig
if [ ! -f $pid ]; then
echo '1' >$pid
if [ $check -gt "$max_load" ]; then
#25 is load average on 5 minutes
cp -rpf $index_php_loc $index_php_loc.bak_ap
echo "$site_maintenance_msg" > $index_php_loc
sleep 15;
if [ $check -gt "$max_load" ]; then
killall -9 httpd
sleep 5;
$apache_init restart
echo "$(date) : Apache Restart due to excessive load | $check |" >> $high_load_log;
cp -rpf $index_php_loc.bak_ap $index_php_loc
rm -f /tmp/$pid
fi
fi
fi