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
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.
×