Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Last active August 12, 2022 19:18
Show Gist options
  • Save SmugZombie/1d2b40e10ca7c71bb56fca56cadf2c82 to your computer and use it in GitHub Desktop.
Save SmugZombie/1d2b40e10ca7c71bb56fca56cadf2c82 to your computer and use it in GitHub Desktop.
Simple cron script to check to see if a docker container is runnign or not and restarts it if not.
#!/bin/bash
# Docker Monitor v1.3
# Simple cron script to check to see if a docker container is runnign or not and restarts it if not.
# Ron Egli <[email protected]>
# Grab the container name from cli argument
CONTAINER_NAME=$1
if [ -z "$CONTAINER_NAME" ]
then
echo $(date -u) "No container specified"
exit
fi
# Check to see if the service is running
servicerunning=$(/usr/bin/docker ps | grep "$CONTAINER_NAME")
if [ -z "$servicerunning" ]
then
# Service is not running. Check to see if Exists
serviceexist=$(/usr/bin/docker ps -a | grep "$CONTAINER_NAME")
if [ -z "$serviceexist" ]
then
# Container in question doesn't exist
echo $(date -u) "Container (" $CONTAINER_NAME ") does not exist"
else
# Container exists, lets start it
echo $(date -u) "Container (" $CONTAINER_NAME ") not running"
/usr/bin/docker start "$CONTAINER_NAME"
fi
else
# Container is already running, nothing to do here
echo $(date -u) "Container (" $CONTAINER_NAME ") is running"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment