Shell script to restart MySQL server if it is killed or not working

by on April 10, 2008 · 6 comments

  1. #!/bin/bash
  2. # Shell script to restart MySQL server if it is killed or not working
  3. # due to ANY causes.
  4. # When script detects mysql is not running (it basically sends ping request
  5. # to MySQL) it try to start using /etc/init.d/mysql script; and it sends an
  6. # email to user indicating the status.
  7. # This script must be run from Cron Job so that it can monitor mysql server.
  8. # For more info visit following url:
  9. # http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html
  10. # --------------------------------------------------------------------------
  11. # Copyright (C) 2005 nixCraft project <http://cyberciti.biz/fb/>
  12. # This script is licensed under GNU GPL version 2.0 or above
  13. # -------------------------------------------------------------------------
  14. # This script is part of nixCraft shell script collection (NSSC)
  15. # Visit http://bash.cyberciti.biz/ for more information.
  16. # -------------------------------------------------------------------------
  17.  
  18. # mysql root/admin username
  19. MUSER="root"
  20. # mysql admin/root password
  21. MPASS="SET-ROOT-PASSWORD"
  22. # mysql server hostname
  23. MHOST="localhost"
  24. #Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
  25. # Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
  26. MSTART="/etc/init.d/mysql start"
  27. # Email ID to send notification
  28. EMAILID="notification@somewhere-corp.com"
  29. # path to mail program
  30. MAILCMD="$(which mail)"
  31. # path mysqladmin
  32. MADMIN="$(which mysqladmin)"
  33.  
  34. #### DO NOT CHANGE anything BELOW ####
  35. MAILMESSAGE="/tmp/mysql.fail.$$"
  36.  
  37. # see if MySQL server is alive or not
  38. # 2&1 could be better but i would like to keep it simple and easy to
  39. # understand stuff :)
  40. $MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
  41. if [ $? -ne 0 ]; then
  42. echo "" >$MAILMESSAGE
  43. echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
  44. echo "Hostname: $(hostname)" >>$MAILMESSAGE
  45. echo "Date & Time: $(date)" >>$MAILMESSAGE
  46. # try to start mysql
  47. $MSTART>/dev/null
  48. # see if it is started or not
  49. o=$(ps cax | grep -c ' mysqld$')
  50. if [ $o -eq 1 ]; then
  51. sMess="MySQL Server MySQL server successfully restarted"
  52. else
  53. sMess="MySQL server FAILED to restart"
  54. fi
  55. # Email status too
  56. echo "Current Status: $sMess" >>$MAILMESSAGE
  57. echo "" >>$MAILMESSAGE
  58. echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
  59. echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
  60. # send email
  61. $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
  62. else # MySQL is running :) and do nothing
  63. :
  64. fi
  65. # remove file
  66. rm -f $MAILMESSAGE


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Branislav Viest

    very good a simple. thx

  • Ahmet Alp

    Very Good. Thank you some much.

  • Murat Çabuk

    Thank you. Best Shell Script.

  • Meskalyn

    Very, very nice script.
    Simple, but so usefull ;)

  • Ed

    Hello

    Thanks for this nice script.
    I’m quite new at server administration, and have 2 questions about your script :
    1- What do you mean by :
    # path to mail program
    MAILCMD=”$(which mail)”
    # path mysqladmin
    MADMIN=”$(which mysqladmin)”

    Do I need to enter the path of both mail and mysql servers ?

    2- If I enter wrong information for the password (and I leave the default intel as question 1), it works …
    So are login and pawd required ?
    Do it works only I 1- is correctly filled ?

    (I’m working with a ovh release 2 (gentoo))

    Thanks
    Ed

  • Mgreen

    Would work better if it check that it could accept commands.

    Something Like….

    mysql -Dmysql -e “SELECT 1;”

    ;)

Previous Script:

Next Script: