Web Server Hack – How To Auto Restart HTTPD service script!

Here’s a simple script I made up that will auto restart your HTTPD service if it reaches a high load.

The script will actually stop your HTTPD server when your load is higher than the maximum load settings and will re-start when the load is back to zero.

If you are on a server that’s overloaded, this script can help you “temporarily” get by instead of your web server dying and you manually rebooting your web server.

There’s also an extra “mail” feature that will email you whenever the server is stopped or restarted. (Code after the first one)

Download the test.txt here (Save it as test.sh)

Download the mail.txt here (Save it as mail.php)

Save both files as test.sh and mail.php, respectively, and put it in the same directory such as /root/ in your linux server where it can’t be seen by the public.

Then run a cron job every couple minutes, something like this in your /etc/cron.d/mycronjob file:

*/10 * * * * root sh /root/test.sh

(This will run every 10 minutes.)

The second code is a simple PHP file that will e-mail. It actually takes the first argument as part of its subject so you can use this PHP code for many other things for notifying you when something happens to your web server.

For test.sh script, you can change the 20 in this line to the maximum load settings before the HTTPD server shuts down:

if [ "$thisloadavg" -ge "20" ]; then

You can also change this line to higher value if you don’t want your server to wait until it reaches load of 0 before restarting:

elif [ "$thisloadavg" -le "1" ]; then

For mail.php, make sure you change the e-mail addresses to your e-mail address for it to work correctly.

You can probably play with this script and come up with something better.  Let me know if you do! 🙂

10 Responses to Web Server Hack – How To Auto Restart HTTPD service script!

  1. davidritterhouse says:

    I get this when I run it:

    check.sh: line 19: [: load: integer expression expected
    check.sh: line 24: [: load: integer expression expected

  2. zedomax says:

    Did u copy from the blog post? Try making sure all the codes are copied right. Sorry, the wordpress won't let me copy and paste correctly.

  3. davidritterhouse says:

    I had to change:
    loadavg=`uptime | awk '{print $9}'`

    to be:
    loadavg=`uptime | awk '{print $10}'`

    It didnt seem to use the else statement. But once I changed that line, all was good.

  4. zedomax says:

    Oh yeah, sometimes the datastamp can throw things off, I forgot to mention.

  5. zedomax says:

    Oh yeah, sometimes the datastamp can throw things off, I forgot to mention.

  6. Suria says:

    My sites root password has been changed after installing this script…please let me know how to recover my root password.. this is script is Virus iguess

  7. max says:

    Your root password change has nothing to do with this script, it’s from something else, I am 100% sure on that.

  8. Suria says:

    wen i saw that word web Server hack.. i thought it really does the hacking anyways..thanks max.. i have reset my password.. i dont know what happend then.. can you assure me this script will automatically restart the apache server wen load is high.. thanks

  9. max says:

    I think it should although you should double-check and test the parameters. I’ve found that Nginx servers are much better than Apache though, they never go overload, which what I have now, you can see instructions on how to install nginx here: http://webserverhacks.com/web-server-hacks/how-to-install-nginx-on-a-wordpress-blog/

  10. Suria says:

    Thanks Max

Leave a Reply

Your email address will not be published.

Check out more interesting categories: Computer, DIY, Fedora, Hack, HOWTO, Linux, Projects, technology, Ubuntu, Web.

Related News and Resources