Linux Web Server Hack – How to Write Automated Load Balancing Script!

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:

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
# restart apache
echo "restart!"
echo "no restart!"
echo "waiting...!"

Save this code somewhere such as /root/

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!

  1. richard staw says:

    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!

  2. max says:

    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.

  3. Hates_ says:

    Yes, but this is not load balancing, this is 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.

  4. BiSScuiTT says:

    O.. M.. G… Best Load Balancer script EVAR!!!!!!! You r Teh L33test!

    Seriously! Can I have some of whatever it is you are smoking !?

  5. inno says:

    The problem is, this isn’t load balancing of any form. It’s throttling, and violently so.

  6. max says:

    Oh awesome, I got it in the Wiki entry now, Bandwidth throttling rocks! 🙂

  7. Pingback: Linux Server Hack - How to Limit Bandwidth with Linux, TC, and iproute2! | - The DIY, HOWTO, Hacks, Gadgets, and Tech Blog!

Leave a Reply

Your email address will not be published.

Check out more interesting categories: Blog, Consumer, DIY, Educational, Featured Hacks, Hack, HOWTO, Misc, Web, Wordpress.

Related News and Resources