Skip to content

Instantly share code, notes, and snippets.

@abdumu
Last active April 23, 2022 15:18
Show Gist options
  • Save abdumu/fbba88ea7292d942bff93e1a354fc360 to your computer and use it in GitHub Desktop.
Save abdumu/fbba88ea7292d942bff93e1a354fc360 to your computer and use it in GitHub Desktop.
check server services and notify you by email or webhook (slack+discord) if service is down and restart didnt work
#!/bin/bash
# check services status
# if that service is not running
# it will start the service and send an email/webhook to you
# if the restart does not work, it sends an email/webhook and then exits
# your email address / discord /slack webhook.
# one at least must be filled
# if we using discord, add "/slack" after webhook as in:
# https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook
EMAIL=""
SLACK_WEBHOOK=""
# list your services you want to check
SERVICES=( 'mysql' 'supervisor' 'redis-server' 'caddy' )
#### OK. STOP EDITING ####
function notify {
if [ ! -z "$EMAIL" ]
then
echo $2 | mail -s "$1" "$EMAIL");
fi
if [ ! -z "$SLACK_WEBHOOK" ]
then
curl -H "Content-Type: application/json" -X POST -d "{\"text\": $1 "\n" $2}" $SLACK_WEBHOOK
fi
}
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
##notify
MESSAGE="$i was down, but I was able to restart it on $(hostname) $(date) "
SUBJECT="$i was down -but restarted- on $(hostname) $(date) "
notify $SUBJECT $MESSAGE
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
##notify
MESSAGE="$i is down on $(hostname) at $(date) "
SUBJECT=" $i down on $(hostname) $(date) "
MESSAGE="${MESSAGE} I tried to restart it, but it did not work"
notify $SUBJECT $MESSAGE
fi
fi
fi
done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment