Bash Script for auto stop apache when load is high

Desperados

Verified User
Joined
Jan 2, 2017
Messages
37
Hello,

i need a Bash script to stop apache when server load is high.

i found this :


#!/bin/bash
loadavg=`uptime | awk '{print $9}'`

if [ "$loadavg" = "average:" ]
then
echo "not this one..."
loadavg=`uptime | awk '{print $10}'`

fi
RESTART="/sbin/service httpd restart"
STOP="/sbin/service httpd stop"
# bash doesn't understand floating point
# so convert the number to an interger

thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`

if [ "$thisloadavg" -ge "10" ]; then
echo "Busy - Load Average $loadavg ($thisloadavg) "
#httpd -k stop
STOP
elif [ "$thisloadavg" -le "1" ]; then
echo "Okay - Load Average $loadavg ($thisloadavg) "
pgrep httpd
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
echo "restart!"
else
echo "no restart!"
fi
else
echo "waiting...!"
fi


does it work and is it harmless ?

Best Regards
 
Hello,

Better install monit from an OS repository and configure it to restart Apache when a CPU is too high. Though it will work I would suggest that you find a root cause of or high server load.

I believe you use Apache + mod_php, that's the most common case when a restart of Apache can help temporary to reduce a server load (unless you suffer under a Slow Loris attack). You really might need to check what is going wrong with PHP and/or MySQL. Probably you have too many heavy SQL queries, or PHP scripts tries to connect to remote services which are not available for any reason, and may connections stay open waiting to die with a time-out.
 
Hello Alex ,
Thanks for you reply, i did them but sometimes on shared hosting some users make load very high and server not access so we should manualy reset the server and this is risky :)

i want stop apache automatic when load is higher than 10 so i can figure the problem and suspend bad account
 
First, the script does not look to be harmful. Then though it seems to be working as you'd expect it, I will not guarantee it is 100% what you need. So it's up to you to test it and see whether it is an acceptable solution.

And I would still suggest using monit, it's running as daemon, no cron is used. It writes logs and can send notifications and alerts to email address of your administrator or/and yours.
 
thanks for the code.

I added a few things. I use it this way. Because wordpress works at php72 (wp-login.php flood increases load)

/etc/init.d/php-fpm72 (my wordpress scripts)
/etc/init.d/php-fpm56 (my other php scripts)
/etc/init.d/php-fpm55 (my other php scripts)
..

crontab
*/5 * * * * root /root/load_control.sh



#!/bin/bash
loadavg=`uptime | awk '{print $9}'`

if [ "$loadavg" = "average:" ]
then
echo "not this one..."
loadavg=`uptime | awk '{print $10}'`

fi
RESTART="/etc/init.d/php-fpm72 restart"
STOP="/etc/init.d/php-fpm72 stop"
# bash doesn't understand floating point
# so convert the number to an interger

thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`

if [ "$thisloadavg" -ge "10" ]; then
echo "Busy - Load Average $loadavg ($thisloadavg) "
#httpd -k stop
STOP
echo “Stop time load $loadavg: `date`” >> /var/log/yukkontrol.load
elif [ "$thisloadavg" -le "1" ]; then
echo "Okay - Load Average $loadavg ($thisloadavg) "
pgrep httpd
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
echo “Restart time load $loadavg: `date`” >> /var/log/yukkontrol.load
echo "restart!"
else
echo “No Restart load $loadavg : `date`” >> /var/log/yukkontrol.load
echo "no restart!"
fi
else
echo "waiting...!"
fi
 
That's good you can write bash scripts. The monit can do it all. And has much more features out of a box.
 
Back
Top