Skip to content

Instantly share code, notes, and snippets.

@sepulworld
Created September 2, 2016 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sepulworld/886cb5edcca6f5c404e47d8873fd18f5 to your computer and use it in GitHub Desktop.
Save sepulworld/886cb5edcca6f5c404e47d8873fd18f5 to your computer and use it in GitHub Desktop.
Service-watchdog
#!/bin/bash
# First arg should be the systemd service to watch
# Requires cloud-utils apt package and pip awscli
# Requires IAM access to IAM action autoscaling:SetInstanceHealth
EC2_INSTANCE_ID=$(ec2metadata --instance-id)
PROCESS_CHECK=$1
REGION=$2
while :
do
if systemctl --no-pager status "$PROCESS_CHECK" | grep -q "active (running)"
then
aws autoscaling set-instance-health --instance-id "$EC2_INSTANCE_ID" --health-status Healthy --region ${REGION}
else
echo "unhealthy, alerting autoscaling group"
aws autoscaling set-instance-health --instance-id "$EC2_INSTANCE_ID" --health-status Unhealthy --region ${REGION}
fi
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment