How To Restart Apache Using cron If the Load Gets Too High

There are times when the hackers slam my servers and the load gets too high. I use a very simple bash script as a cron job to monitor the load and take action when it gets too high.
The script will stop httpd, sleep for a configurable period of time (currently 3 minutes), then start httpd.
As always, YMMV!
Add this to the root cron, making sure the paths match your environment:
* * * * * /root/restart_apache_if_load_hits_threshold.sh >> /var/log/restart_apache_if_load_hits_threshold.log 2>&1
restart_apache_if_load_hits_threshold.sh
(Be sure to modify the email address in two places…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/bin/bash MAX=6 HOST=`hostname` SLEEP=180 check=$(uptime | awk -F'average: ' '{print $2}' | awk '{print $1}' | tr -d ',') if [[ $check > $MAX ]]; then /sbin/service httpd stop echo $HOST | mailx -s "$HOST load alert - apache stopped by cron script" load.alert@yourDomain.com sleep $SLEEP /sbin/service httpd start echo $HOST | mailx -s "$HOST load alert - apache started by cron script" load.alert@yourDomain.com fi |
Leave Your Comment
All fields marked with "*" are required.