Skip to content

Instantly share code, notes, and snippets.

@djaney
Created February 20, 2015 15:52
Show Gist options
  • Select an option

  • Save djaney/270e6799e6aaa2a288dd to your computer and use it in GitHub Desktop.

Select an option

Save djaney/270e6799e6aaa2a288dd to your computer and use it in GitHub Desktop.
#!/bin/bash
#ver. 2
##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="xxxxxxxxxxxx"
##list your services you want to check
SERVICES=( 'hhvm' 'nginx' 'mysql' )
#### DO NOT CHANGE anything BELOW ####
for i in "${SERVICES[@]}"
do
###IF SERVICE IS NOT RUNNING####
service $i status;
STATS=$(echo $?)
echo $STATS
if [[ $STATS != 0 ]]
then
##TRY TO RESTART THAT SERVICE###
service $i restart
##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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment