Bash Script for Checking Services

August 21, 2016 585 views
Linux Basics Linux Commands Ubuntu 16.04

I am re-writing a services-checking script:
https://github.com/sierracircle/services-checker

The original stopped working on Ubuntu 16.04, so I had to re-jigger it. So far so good; however, I would like to add a new feature.

Currently, the script checks all services you list for whatever interval you set your cronjob to.

  • If the service is running, all good.
  • If the service is stopped, it attempts to restart the service.
  • If the service is running after the restart, it sends you an email telling you that it restarted.
  • If the service is not running after the restart attempt, it sends an email telling you that the service is down and was not able to restart.

...but, if the last thing happens, it goes through the same loop again and again, so you might wake up in the morning with hundreds of emails telling you that it was unable to restart that service.

I would like to change this script to only attempt to restart 3 times, and then leave it. That way, you get 3 emails.

Any clues on a simple way to do that?

Here is the loop the script goes through. It does this loop for each service you define:

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
##SEND AN EMAIL###
MESSAGE="$i   is down, but looks like I was able to restart it on   on $(hostname) $(date)  "
SUBJECT="$i  down -but restarted-  on $(hostname) $(date) "
echo   $MESSAGE | mail -s "$SUBJECT" "$EMAIL"

else
##IF RESTART DID NOT WORK 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
done
1 Answer
ryanpq MOD August 22, 2016
Accepted Answer

I would think you would need to track this outside of the loop since whether this case is true would have to be remembered between runs of this loop. The easiest way to do this might be to create a temporary file somewhere if the service has failed to start. Then on future loops you can check for that file's existence. If it exists your script can know not to attempt to restart the service or send an email again.

  • Thanks for the reply @ryanpq

    I tested your idea and it works. I chose to create a file in the /tmp folder, since it will be deleted if the server is rebooted and start the process fresh.

    so now the script does the following:

    • checks if service is running
    • if not->attempts restart
    • if restarted-> sends email that it was restarted
    • if restart did not work->checks for tmp file
    • if no tmp file->creates tmp file and sends email that the service is not running
    • if tmp file exists->exits

    The above works fine for me, because it will continue to try to restart the service but stop sending emails until the service is restarted.

    Great idea!

     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   is down, but looks like I was able to restart it on   on $(hostname) $(date)  "
                    SUBJECT="$i  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"
                    else
                        exit 0;
                    fi
            fi
        fi
      done
    
Have another answer? Share your knowledge.