shell script to restart mysql server if it is killed or not working

Upload: ikhwani-fill-ilmi

Post on 03-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Shell Script to Restart MySQL Server if It is Killed or Not Working

    1/2

    #!/bin/bash# Shell script to restart MySQL server if it is killed or not working# due to ANY causes.# When script detects mysql is not running (it basically sends ping request# to MySQL) it try to start using /etc/init.d/mysql script; and it sends an# email to user indicating the status.# This script must be run from Cron Job so that it can monitor mysql server.# For more info visit following url:# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html# --------------------------------------------------------------------------# Copyright (C) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above# -------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC)# Visit http://bash.cyberciti.biz/ for more information.# -------------------------------------------------------------------------# mysql root/admin usernameMUSER="root"# mysql admin/root passwordMPASS="SET-ROOT-PASSWORD"# mysql server hostnameMHOST="localhost"

    #Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.MSTART="/etc/init.d/mysql start"# Email ID to send notificationEMAILID="[email protected]"# path to mail programMAILCMD="$(which mail)"# path mysqladminMADMIN="$(which mysqladmin)"#### DO NOT CHANGE anything BELOW ####MAILMESSAGE="/tmp/mysql.fail.$$"

    # see if MySQL server is alive or not# 2&1 could be better but i would like to keep it simple and easy to# understand stuff :)$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/nullif [ $? -ne 0 ]; then

    echo "" >$MAILMESSAGEecho "Error: MySQL Server is not running/responding ping request">>$MAIL

    MESSAGEecho "Hostname: $(hostname)" >>$MAILMESSAGEecho "Date & Time: $(date)" >>$MAILMESSAGE# try to start mysql$MSTART>/dev/null

    # see if it is started or noto=$(ps cax | grep -c ' mysqld$')if [ $o -eq 1 ]; then

    sMess="MySQL Server MySQL server successfully restarted"else

    sMess="MySQL server FAILED to restart"fi# Email status tooecho "Current Status: $sMess" >>$MAILMESSAGEecho "" >>$MAILMESSAGE

  • 7/28/2019 Shell Script to Restart MySQL Server if It is Killed or Not Working

    2/2

    echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE

    echo "*** Please don't reply this email, this is just notification email***" >>$MAILMESSAGE

    # send email$MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE

    else # MySQL is running :) and do nothing:

    fi# remove filerm -f $MAILMESSAGE