Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # function restartDockerByName()
- # requires a container name be passed to it
- function restartDockerByName(){
- for j in $(docker container ls -a | grep $1 | cut -c1-12)
- do
- # this is a 'restart function'
- # so make sure the docker is already running
- # don't act on it otherwise
- szStatus=`docker inspect -f '{{.State.Running}}' $j`
- if [ "$szStatus" = "true" ]
- then
- #echo "$j state: Not Running"
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 is running"
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 stopping"
- docker stop $j > /dev/null 2>&1
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 stopped"
- ##
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 starting"
- docker start $j > /dev/null 2>&1
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 started"
- echo ''
- else
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: $1 is not running"
- echo ''
- fi
- done
- }
- # szContainers: list of dockers to restart
- szContainers=('container1' 'container2');
- szCount=${#szContainers[@]};
- # Check if docker is running
- if docker info > /dev/null 2>&1; then
- echo ''
- #echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: dockeris running"
- #echo ''
- #loop thru szContainers
- for i in ${szContainers[@]}; do
- echo "Status: Restarting $i"
- # restartDockerbyName <containername>
- restartDockerByName $i
- done
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: finished exiting"
- else
- echo "`date "+%Y-%m-%d %H:%M:%S"`" "Status: docker is not running exiting"
- exit 1
- fi
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement