Question

Bash Script for Checking Services

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

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.