Skip to content

Instantly share code, notes, and snippets.

@egalink
Last active January 12, 2024 18:10
Show Gist options
  • Select an option

  • Save egalink/4a3787326e35de6aaecc403c6a0fba3b to your computer and use it in GitHub Desktop.

Select an option

Save egalink/4a3787326e35de6aaecc403c6a0fba3b to your computer and use it in GitHub Desktop.
Checks if a docker container exists and is running. This script is adapted to use in a crontab.
#!/bin/bash
# check_and_run_container_from_crontab.sh
# chmod +x check_and_run_container_from_crontab.sh
# @reboot /path/to/check_and_run_container_from_crontab.sh my_container_name
container_name=$1
if docker container inspect "$container_name" &>/dev/null; then
echo "Container $container_name already exists, checking status..."
if $(docker inspect -f '{{.State.Status}}' "$container_name" | grep -q "running"); then
echo "The container $container_name is running."
else
echo "The container $container_name is not running. Starting it..."
docker start "$container_name"
docker ps -a
fi
else
echo "Container $container_name does not exist, did you create the container?"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment