Auto Restart Apache/Mysql Service on High Load

Sometime our server hang on high load which cause ssh hang so we are not able to login and check any service which are taking high memory or CPU nd also it cases our website down for some time. So we need a auto script which can restart services when taking too much load on server. This below script use for Auto Restart Apache/Mysql Service on High Load whatever take high load on cpu or ram and send mail so that details can be check on server on later or that time.

 

#!/bin/sh

    # location of index.php to overwrite with temporary message   
    index_php_loc='sitepath/index.php';        default /var/www/index.php
    [email protected]

    # script to check server for extremely high load and restart Apache if the condition is matched

    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 to Apache init script

    apache_init='/etc/init.d/apache2';

    #

    site_maintenance_msg="Site Maintenance in progress - We will be back online in a minute";

    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

    $apache_init stop

    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
    echo "service restart on high load please check the server"|mail -s "Apache Restart on high load" $mailid

    fi

    fi

 

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *