• Home
  • Services
    • HR Services
      • HR Advisory Services
      • Contract Staffing
      • HR Outsourcing Services
      • Manpower Supply Services
      • Payroll Processing
      • Permanent Placement
      • Recruitment and Placement Services
      • Recruitment Process Outsourcing
      • Staffing Agency Services
    • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
    • Website Maintenance
    • Company Registration Services
    • Virtual Office Space Address
  • Company
    • FAQs
    • About Us
    • Contact
  • Locations
  • Blogs
    • Blog
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum

    Greenplum

    $1,500.00
    Read More
    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    Login
    RayaFeeL
    • Home
    • Services
      • HR Services
        • HR Advisory Services
        • Contract Staffing
        • HR Outsourcing Services
        • Manpower Supply Services
        • Payroll Processing
        • Permanent Placement
        • Recruitment and Placement Services
        • Recruitment Process Outsourcing
        • Staffing Agency Services
      • DBA Support
        • DBA Consultancy Services
        • PostgreSQL Support
      • Website Maintenance
      • Company Registration Services
      • Virtual Office Space Address
    • Company
      • FAQs
      • About Us
      • Contact
    • Locations
    • Blogs
      • Blog
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum

      Greenplum

      $1,500.00
      Read More

      Blog

      • Home
      • Blog
      • Blog
      • Mail Alert Notification for MariaDB Service is Down

      Mail Alert Notification for MariaDB Service is Down

      • Posted by 2ndnijam
      • Categories Blog
      • Date May 11, 2019
      • Comments 0 comment

      This script can be used to check on mariaDB, apache2, or whatever service you want. This is not meant to be a SOLUTION to services that are crashing, but rather a notification and a temporary restart until you can solve the real issue.

      The script will check the status of each service. If the service is stopped, it tries to restart the service. If the service starts, it sends you an email saying the service stopped but was restarted.

      If the service does not start for some reason, it sends you an email telling you it was not started.

      After that, it will continue to try and start, but not send any more emails until the service is finally started.

      NOTE*: This script should be run as your root user, so it would be added to the crontab like so:

      1. put it into your scripts folder
      2. set your email address
      3. set the services you want to keep an eye on (by default it has MariaDB and apache2..you can add or take away whatever you need)
      4. save your changes
      5. create a cronjob as root (sudo crontab -e) and add something like this, which runs every minute (adjust to your needs):
      #check on services
      */1 *  * * * sh /root/script/dbalert.sh

       

      ################### DOWN DB server######################
      #!/bin/bash
      #ver. 2
      
      ##this script will check whatever services
      ##you want to keep an eye on
      ##if that service is not running
      ##it will (try to) start the service and send
      ##an email to you
      
      ##set your email address
      EMAIL="nijamutheen@gmail.com"
      
      ##list your services you want to check
      ##you can add as many as you like
      SERVICES=('mysql')
      
      #### DO NOT CHANGE anything BELOW ####
      
      
       for i in "${SERVICES[@]}"
        do
          ###IF SERVICE IS NOT RUNNING####
      if ([[ "$(service $i status)" =~ "not running" ]] || [[ "$(service $i status)" =~ "is stopped" ]] || [[ $"$(service $i status)" =~ "stop/waiting" ]])
      
          then
             ##TRY TO RESTART THAT SERVICE###
          service $i start
      
              ##IF RESTART WORKED###
      if ([[ "$(service $i status)" =~ "MariaDB running" ]] || [[ "$(service $i status)" =~ "is started" ]] || [[ $"$(service $i status)" =~ "start" ]])
      then
              ##SEND AN EMAIL###
          MESSAGE="$(tail -15 /var/lib/mysql/publishman.com.err)"
          SUBJECT="MariaDB down But restarted Successfully on $(date) "
          echo -e "   LOGS :  \n$MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
             else
      
              ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
          MESSAGE="$(tail -15 /var/lib/mysql/publishman.com.err)"
          SUBJECT="MariaDB down, Restarted did not work on $(date) "
          echo -e "   LOGS :  \n$MESSAGE . \n>>>>> Script tried to restart but it did not work"  | mail -s "$SUBJECT" "$EMAIL"
      
             fi
          fi
      
      done
      
                        ################### DOWN APACHE server######################
      #!/bin/bash
      #ver. 2
      
      ##this script will check whatever services
      ##you want to keep an eye on
      ##if that service is not running
      ##it will (try to) start the service and send
      ##an email to you
      
      ##set your email address
      EMAILA="nijamutheen@gmail.com"
      
      ##list your services you want to check
      ##you can add as many as you like
      SERVICESA=('httpd')
      
      #### DO NOT CHANGE anything BELOW ####
      
      
       for a in "${SERVICESA[@]}"
        do
          ###IF SERVICE IS NOT RUNNING####
      if ([[ "$(service $a status)" =~ "not running" ]] || [[ "$(service $a status)" =~ "is stopped" ]] || [[ $"$(service $a status)" =~ "stop/waiting" ]])
      
      #if [[  "$(service $a status)" =~ "$a is stopped"  ]]
      
          then
             ##TRY TO RESTART THAT SERVICE###
          service $a start
      
              ##IF RESTART WORKED###
      #if [[  "$(service $a status)" =~ "is running"  ]]
      if ([[ "$(service $a status)" =~ "is running" ]] || [[ "$(service $a status)" =~ "is started" ]] || [[ $"$(service $a status)" =~ "start" ]])
          then
              ##SEND AN EMAIL###
          MESSAGEA="$(tail -15 /var/log/apache2/error_log)"
          SUBJECTA="APACHE SERVER down But restarted successfully on $(date) "
          echo -e "   LOGS :  \n$MESSAGEA "  | mail -s "$SUBJECTA" "$EMAILA"
             else
      
              ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
          MESSAGEA="$(tail -15 /var/log/apache2/error_log)"
          SUBJECTA="APACHE SERVER down restarted did not work on $(date) "
          echo -e  "   LOGS:  \n$MESSAGEA  . \n>>>>> Script tried to restart but it did not work"  | mail -s "$SUBJECTA" "$EMAILA"
      
             fi
          fi
      done

      Tag:mail notification, MariaDB, script

      • Share:
      2ndnijam

      Previous post

      Nijamutheen Resume - For PostgreSQL ,Greenplum, Oracle, MySQL , Mariadb, MSSQL, AWS, Linux admin job
      May 11, 2019

      Next post

      MariaDB Backup Script Mail Notifications Alert
      May 12, 2019

      Leave A Reply

      You must be logged in to post a comment.

      Connect with



      Search

      ADVERTISEMENT

      Latest Posts

      Tips to Choose the Best Serviced Office for Your Business
      24May2022
      What key considerations do you need to keep in mind when choosing a new serviced office to house your business?
      24May2022
      The Benefits of Coworking
      24May2022
      The Long-Term Impact of Coworking
      24May2022
      Are you spending more money than you earn? Outsource with Ease and Move When You’re Ready
      24May2022
      PostgreSQL

      PostgreSQL

      $800.00 $500.00
      Greenplum

      Greenplum

      $1,500.00
      Oracle Database

      Oracle Database

      $350.00
      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Our Team

      COURSES

      • List Of Course
      • Become An Instructor

      Support

      • DBA Support
      • Consultancy Services

      Recommend

      • Login
      • Contact Us

      IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

      • Privacy
      • Terms

      Become An Instructor?

      Join thousand of instructors and earn money hassle free!

      Get Started Now

      Connect with

      Login with Google Login with Twitter Login with Linkedin Login with Windowslive Login with Yahoo

      Login with your site account

      Connect with



      Lost your password?