Well, I have been manually managing the dedicated server for this site for last 2 years or so but I found a better way to automate the server so it doesn’t ever go down due to overload.
One of the common problems in dedicated servers is the fact that load can go out of control and your web server along with it, causing you to hard-restart the server.
A better way to deal with this over-load problem is to shut down the HTTPD server (web server) before your server load gets to something around 2 to 5.
When the server load drops under 2, the script then can restart the server.
By doing this, you save a lot of headaches especially if you get “digged” or simply need the site up as much as possible.
Believe it or not, there’s no script that does this available free online so I made one for all you webmasters:
#!/bin/bash
loadavg=`uptime | awk '{print $9}'`
RESTART="/sbin/service httpd restart"
# bash doesn't understand floating point
# so convert the number to an interger
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`
if [ "$thisloadavg" -ge "5" ]; then
echo "Busy - Load Average $loadavg ($thisloadavg) "
httpd -k stop
elif [ "$thisloadavg" -le "2" ]; 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
Save this code somewhere such as /root/checkload.sh.
Then add the following to your cron job. (/etc/cron.d/sa-update for Fedora Linux)
*/1 * * * * root sh /root/checkloadsh
This will run the script every 1 minute to check the load and if load is too high, web server will be turned off, if it’s lower than 2 and web server is off, it will turn the web server on.
There’s a lot of things that can go wrong with your web server whether from extra traffic or whatnot, but this script will be handy and I do recommend it to anyone who’s having trouble with high-load web servers that go down often. (like this blog)
Now, go install this script, you will never have to worry about your web server dying from high load ever again.
You can also change the value of “2” in the code to something higher such as 5 or 10, which will wait longer to shut the web server off if load goes high.
I didn’t write the code from scratch, I took 2-3 different scripts and mixed it up so here’s the resources I used for the code:
Load Average Script – This one is the main skeleton I used
Check/Restart HTTPD Script – This one I used to check HTTPD server before trying to restart it.
Cron Task Scheduler – I keep forgetting that the most left number is the minutes, darn.
NOTE – This is a simple solution to keep your dedicated server running on 1 server (like this one), if you can afford more servers, you will need to resort to load balancing the “servers”, load balancing here refers to balancing within 1 server.
7 Responses to Linux Web Server Hack – How to Write Automated Load Balancing Script!
Leave a Reply

That is not load balance! it is just shutdown the webserver on higher loads, not a good pratice at all, check for linux virtual server, pound and even iptables can do load balance!
Well, I am talking about the “cheap” solution to load balance, of course if you had money for multiple servers, that’d be another story.
Besides, iptables can’t do load balance in a simple way,
this is a great solution for people who can afford 1 dedicated server. (like this one but believe me I’d need at least 2 servers but running on one here)
It’s like you are saying you can get a high-powered turbo for your car but most people can’t afford it and it’s not a viable solution if you have to pay $2000/month.
Yes, but this is not load balancing, this is throttling:
http://en.wikipedia.org/wiki/Bandwidth_throttling
Even on one server you can use proper load balancing techniques. Like running a small cluster and directing requests to non-busy servers via a proxy of some sort like Nginx.
O.. M.. G… Best Load Balancer script EVAR!!!!!!! You r Teh L33test!
Seriously! Can I have some of whatever it is you are smoking !?
The problem is, this isn’t load balancing of any form. It’s throttling, and violently so.
Oh awesome, I got it in the Wiki entry now, Bandwidth throttling rocks! 🙂
Pingback: Linux Server Hack - How to Limit Bandwidth with Linux, TC, and iproute2! | zedomax.com - The DIY, HOWTO, Hacks, Gadgets, and Tech Blog!