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! 🙂