How To Auto-Restart Apache If the Load Gets Too High

Published Date Author: , Posted September 15th, 2015 at 7:52:39pm

The script to check and restart apache…works on CentOS – YMMV!

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
MAXLOAD=6
check=$(uptime | awk -F'average: ' '{print $2}' | awk '{print $1}' | tr -d ',')
if [[ $check > $MAXLOAD ]]; then
    /sbin/service httpd stop
    HOST=`hostname`
    echo $HOST | mailx -s "$HOST load alert - apache STOPPED by cron script" load.alert@your.domain
    sleep 300
    /sbin/service httpd start
    echo $HOST | mailx -s "$HOST load alert - apache STARTED by cron script" load.alert@your.domain
fi
#!/bin/bash
MAXLOAD=6
check=$(uptime | awk -F'average: ' '{print $2}' | awk '{print $1}' | tr -d ',')
if [[ $check > $MAXLOAD ]]; then
	/sbin/service httpd stop
	HOST=`hostname`
	echo $HOST | mailx -s "$HOST load alert - apache STOPPED by cron script" load.alert@your.domain
	sleep 300
	/sbin/service httpd start
	echo $HOST | mailx -s "$HOST load alert - apache STARTED by cron script" load.alert@your.domain
fi

Add to cron

* * * * * /root/restart_apache_if_load_hits_threshold.sh >> /var/log/restart_apache_if_load_hits_threshold.log 2>&1

No comments as yet.

Leave Your Comment  Leave a comment

All fields marked with "*" are required.