Monday, May 7, 2012

Script to Check the load of webserver and Restart Services

The script below is done in Bash, which can be used across all Linux platforms. The function of the script is to check the web-server load and restart the normal services which reduces the load. It restarts apache and mysql. You can custom this to restart your services.

1. Navigate to /root.
$ cd /root

2. Create a new file
$ vim load.sh
or
$ vi load.sh

3. Insert the following lines to the file
-----------------------------------
LOAD=`uptime|awk '{print $10}'|cut -d, -f1 |cut -d. -f1`
if [ $LOAD -ge 50 ]
then
killall -9 mysqld
killall -9 mysqld
killall -9 mysqld
killall -9 httpd
killall -9 httpd
killall -9 httpd
for SEMID in `ipcs -s httpd| awk '{print $2}'| grep -iv sem`; do ipcrm -s $SEMID; done
/etc/init.d/httpd start
sleep 20
/etc/init.d/mysqld start
echo high load $LOAD at `date`, mysql/apache restarted >> /usr/monitor/mysql.log
fi
-----------------------------------

4.  Give execute permission.
$ chmod 755  load.sh

5. Set a cron to check it at regular intervals.
$ crontab -e
*/5 * * * * sh -x load.sh
(This will check every 5 minutes)

3 comments:

Sujoy dhar said...

what is the process usage in this ?

is it this ?

if [ $LOAD -ge 50 ]

Unknown said...

This will alert only if load is greater than 50

Unknown said...

This will alert only if load is greater than 50

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews