Showing posts with label restart services. Show all posts
Showing posts with label restart services. Show all posts

Monday, May 7, 2012

Script to check if mysql is alive and restart if its not.

The script below is done in Bash, which can be used across all Linux platforms. The function of the script is to check if mysql is currently running properly else it will restart the service.


1. Navigate to /root.
$ cd /root

2. Create a new file
$ vim mysql.sh
or
$ vi mysql.sh
Also give the command below to check the logs
$ touch /root/mysql.log

3. Insert the following lines to the file
-----------------------------------
#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
        /etc/init.d/mysqld stop
        /etc/init.d/mysqld start
        echo Starting at `date`
fi
-----------------------------------

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

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

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)

Twitter Delicious Facebook Digg Stumbleupon Favorites More

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