0 like 0 dislike
114 views
asked in MySQL Database Forum by (4k points)  
any shell script to send email alert and restarting automatically if service is down  the services are mysql ,apache...etc

Please log in or register to answer this question.

4 Answers

0 like 0 dislike
answered by (4k points)  
selected by
 
Best answer

I am working on a script that:

  • checks if mysql and apache are running
  • if not, it sends an email and then attempts to restart the non-running service

I would like to add the last few lines of the error logs to the email. Here is what I have so far. The check works, the restart works, sending email works, but am not getting my error messages from the log:

#!/bin/bash
#ver. 1 

##this script will check mysql and apache
##if that service is not running
##it will start the service and send 
##an email to you

##set your email address 
EMAIL="email@yourdomain.com"

##list your services you want to check
SERVICES=(  'mysql' 'apache2' )

#### DO NOT CHANGE anything BELOW ####

 for i in "${SERVICES[@]}"
  do
    ###IF APACHE IS NOT RUNNING####
     if [[  "$(service $i status)" =~ "not"  ]]
    then
    service $i start
    MESSAGE= "$(tail -5 /var/log/$i/error.log)"
    SUBJECT="$i down on $(hostname) $(date) "
    echo "$MESSAGE   mysql" | mail -s "$SUBJECT" "$EMAIL"
     fi
    ###IF MYSQL IS NOT RUNNING####
     if [[  "$(service $i status)" =~ "stop" ]]
    then
    service $i start
    MESSAGE="$(tail -5 /var/log/$i/error.log)"
    SUBJECT="$i down on $(hostname) $(date) "
    echo "$MESSAGE " | mail -s "$SUBJECT" "$EMAIL"
     fi
  done

Any tips on getting info from the error logs into the email?
Also, my loop would be much better if all services had the same status message...I wonder if there is a better way of checking if the service is running that will return the same result for all services.

0 like 0 dislike
answered by (4k points)  
edited by

This script can be used to check on mysql, 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 mysql 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

 

#!/bin/bash
#ver. 4
##this script will check mysql and apache
##if that service is not running
##it will start the service and send an email to you
##if the restart does not work, it sends an email and then exits

##set the path ##this works for Ubuntu 14.04 and 16.04
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

##set your email address 
EMAIL="nijam@2ndquadrant.in"

##list your services you want to check
SERVICES=( 'httpd' 'mysql' )  ## CentOS apache2 called httpd, if OS is other Linux you can give as apache2

#### OK. STOP EDITING ####


 for i in "${SERVICES[@]}"
  do
 ###CHECK SERVICE####
 `pgrep $i >/dev/null 2>&1`
 STATS=$(echo $?)

 ###IF SERVICE IS NOT RUNNING####
 if [[  $STATS == 1  ]]

  then
  ##TRY TO RESTART THAT SERVICE###
  service $i start

  ##CHECK IF RESTART WORKED###
  `pgrep $i >/dev/null 2>&1`
  RESTART=$(echo $?)

  if [[  $RESTART == 0  ]]
   ##IF SERVICE HAS BEEN RESTARTED###
   then

    ##REMOVE THE TMP FILE IF EXISTS###
    if [ -f "/tmp/$i" ]; 
    then
     rm /tmp/$i
    fi

    ##SEND AN EMAIL###
    MESSAGE="$i was down, but I was able to restart it on $(hostname) $(date)  "
    SUBJECT="$i was down -but restarted-  on $(hostname) $(date) "
    echo $MESSAGE | mail -s "$SUBJECT" "$EMAIL"

   else
    ##IF RESTART DID NOT WORK###

    ##CHECK IF THERE IS NOT A TMP FILE###
    if [ ! -f "/tmp/$i" ]; then

     ##CREATE A TMP FILE###
     touch /tmp/$i

     ##SEND A DIFFERENT EMAIL###
     MESSAGE="$i is down on $(hostname)  at $(date)  "
     SUBJECT=" $i  down on $(hostname) $(date) "
     echo $MESSAGE  " I tried to restart it, but it did not work"  | mail -s "$SUBJECT" "$EMAIL"
    fi
  fi
 fi
  done
exit 0;

Alternate script for alerting services are down :

#!/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="mail@yourdomain.com"

##list your services you want to check
##you can add as many as you like
SERVICES=(  'mysql' 'apache2' )

#### DO NOT CHANGE anything BELOW ####


 for i in "${SERVICES[@]}"
  do
    ###IF SERVICE IS NOT RUNNING####

    service $i status;
    STATS=$(echo $?)
    if [[  $STATS == 3  ]]

    then
       ##TRY TO RESTART THAT SERVICE###
    service $i start

        ##IF RESTART WORKED###
        service $i status;
        RESTART=$(echo $?)
       if [[  $RESTART == 0  ]]

    then
        ##SEND AN EMAIL###
    MESSAGE="$(tail -5 /var/log/$i/error.log)"
    SUBJECT="$i down -but restarted-  on $(hostname) $(date) "
    echo "   $i  $MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
       else

        ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
    MESSAGE="$(tail -5 /var/log/$i/error.log)"
    SUBJECT="$i down on $(hostname) $(date) "
    echo "   $i  $MESSAGE  . I tried to restart but it did not work"  | mail -s "$SUBJECT" "$EMAIL"

       fi
    fi

  done

 

0 like 0 dislike
answered by (4k points)  

                  # DOWN DATABASE !!!!!!!!!!!!!!!!!!!!!!!!! #
#!/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####

    service $i status;
    STATS=$(echo $?)
    if [[  $STATS == 3  ]]

    then
       ##TRY TO RESTART THAT SERVICE###
mv /var/lib/mysql/ib_logfile* /tmp/
    service $i start

        ##IF RESTART WORKED###
        service $i status;
        RESTART=$(echo $?)
       if [[  $RESTART == 0  ]]

    then
        ##SEND AN EMAIL###
    MESSAGE="$(tail -10 /var/lib/mysql/vps.publishman.com.err)"
    SUBJECT="$i down -but restarted-  on $(hostname) $(date) "
    echo "   $i  $MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
       else

        ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
    MESSAGE="$(tail -5 /var/lib/mysql/2ndquadrant,in.err)"
    SUBJECT="$i down on $(hostname) $(date) "
    echo "   $i  $MESSAGE  . I tried to restart but it did not work"  | mail -s "$SUBJECT" "$EMAIL"

       fi
    fi

  done

                  # DOWN APACHE !!!!!!!!!!!!!!!!!!!!!!!!! #
#!/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=('httpd')

#### DO NOT CHANGE anything BELOW ####


 for i in "${SERVICES[@]}"
  do
    ###IF SERVICE IS NOT RUNNING####

    service $i status;
    STATS=$(echo $?)
    if [[  $STATS == 3  ]]

    then
       ##TRY TO RESTART THAT SERVICE###
    service $i start

        ##IF RESTART WORKED###
        service $i status;
        RESTART=$(echo $?)
       if [[  $RESTART == 0  ]]

    then
        ##SEND AN EMAIL###
    MESSAGE="$(tail -10 /var/log/apache2/error_log)"
    SUBJECT="$i down -but restarted-  on $(hostname) $(date) "
    echo "   $i  $MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
       else

        ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL##
    MESSAGE="$(tail -5 /var/log/apache2/error_log)"
    SUBJECT="$i down on $(hostname) $(date) "
    echo "   $i  $MESSAGE  . I tried to restart but it did not work"  | mail -s "$SUBJECT" "$EMAIL"

       fi
    fi

  done

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
Welcome to PostgreSQL Database Discussion Forum where you can ask questions and receive answers from other members of the community. Can discuss here Oracle, Postgresql, mariadb , mySQL , AWS , Linux , MSSQL , MongoDB , Greenplum databases related queries ...etc.
...