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

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. # path to mail program
  29. MAILCMD="$(which mail)"
  30. # path mysqladmin
  31. MADMIN="$(which mysqladmin)"
  32.  
  33. #### DO NOT CHANGE anything BELOW ####
  34. MAILMESSAGE="/tmp/mysql.fail.$$"
  35.  
  36. # see if MySQL server is alive or not
  37. # 2&1 could be better but i would like to keep it simple and easy to
  38. # understand stuff :)
  39. $MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
  40. if [ $? -ne 0 ]; then
  41. echo "" >$MAILMESSAGE
  42. echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
  43. echo "Hostname: $(hostname)" >>$MAILMESSAGE
  44. echo "Date & Time: $(date)" >>$MAILMESSAGE
  45. # try to start mysql
  46. $MSTART>/dev/null
  47. # see if it is started or not
  48. o=$(ps cax | grep -c ' mysqld$')
  49. if [ $o -eq 1 ]; then
  50. sMess="MySQL Server MySQL server successfully restarted"
  51. else
  52. sMess="MySQL server FAILED to restart"
  53. fi
  54. # Email status too
  55. echo "Current Status: $sMess" >>$MAILMESSAGE
  56. echo "" >>$MAILMESSAGE
  57. echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
  58. echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
  59. # send email
  60. $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
  61. else # MySQL is running :) and do nothing
  62. :
  63. fi
  64. # remove file
  65. rm -f $MAILMESSAGE

Latest Post

Popular Post

Our Skills

Author

Project Director Information Architect Database Administrator Unix Administrator Senior Developer Project Manager Product Manager Web Marketer Cloud Engineer

Contact Us

For any questions or project ideas please contact me at the following.

I'm here