Skip to content

Instantly share code, notes, and snippets.

@a7madM
Last active October 22, 2020 15:50
Show Gist options
  • Save a7madM/31acf45d05fb309dad76442b04c0fe92 to your computer and use it in GitHub Desktop.
Save a7madM/31acf45d05fb309dad76442b04c0fe92 to your computer and use it in GitHub Desktop.
Monitor containers and restart not working ones
#!/bin/bash
# Reference https://gist.github.com/haukurk/a6e0751a8b8746265f8b2c55d9476230
declare -a containers
containers[0]=container_name_1
containers[1]=container_name_2
containers[2]=container_name_3
# more containers...
for i in "${containers[@]}"; do
RUNNING=$(docker inspect --format="{{ .State.Running }}" $i 2>/dev/null)
if [ $? -eq 1 ]; then
echo "UNKNOWN - $CONTAINER does not exist."
exit 3
else
Address=$(docker inspect -f {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}} $i)
echo "Check $i"
if [ "$(curl -sL -w '%{http_code}' $Address -o /dev/null)" = "200" ]; then
echo "Success"
else
docker restart $i
echo "Fail"
fi
fi
if [ "$RUNNING" == "false" ]; then
docker restart $i
echo "CRITICAL - $CONTAINER is not running."
exit 2
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment