當前位置 主頁 > 服務器問題 > Linux/apache問題 > 最大化 縮小

    使用Shell腳本批量啟停Docker服務

    欄目:Linux/apache問題 時間:2019-11-19 10:15

    最近日常測試中經常需要手動啟動或停止docker,于是決定寫一個Shell腳本來代替人工操作,另外該腳本,也可以通過Python腳本實行遠程調用,詳細如下所示:

    目前該腳本是將Container ID寫死在腳本中,當然也可以通過傳參給腳本來進行控制,大家可以改造一下。

    啟動docker

    啟動腳本詳細如下所示:

    #!/bin/bash
    containerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
    statusLived="live"
    statusdead="Dead"
    notExistContainer="None"
    retryCount=3
    function GetContainerStatus(){
     containerExist=$(sudo docker ps -a | grep -i $1 | wc -l ) 
     if [ ${containerExist} -gt 0 ]
      then
      pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
      if [ "${pid}" != "0" ]
       then 
       echo "${statusLived}"
      else
       echo "${statusdead}"
      fi
     else
      echo "${notExistContainer}" 
     fi
    }
    function StartContainer(){
     sudo docker restart $1
    }
    for containerID in ${containerIDs}
     do
     for((i=1;i<=${retryCount};i++))
     do
     status=$(GetContainerStatus ${containerID} )
     echo "Container ${containerID} status is ${status}"
     if [ "${status}" == ${statusLived} ]
      then
      echo "Container ${containerID} already running"
      break
     fi
     if [ "${status}" == ${notExistContainer} ]
      then
      echo "Container ${containerID} not existed"
      break
     fi
     if [ "${status}" == ${statusdead} ]
      then
      echo "Container ${containerID} stopped ,start container"
      StartContainer ${containerID}
      verifyStatus=$(GetContainerStatus ${containerID} )
      if [ "${verifyStatus}" == ${statusLived} ]
       then
        echo "start container ${containerID} success "
        break
      else
       echo "${i} retry start container"
       StartContainer ${containerID}
      fi
     fi
     done
    done

    停止docker

    停止腳本詳細如下所示:

    #!/bin/bash
    containerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
    statusLived="live"
    statusdead="Dead"
    notExistContainer="None"
    retryCount=3
    function GetContainerStatus(){
     containerExist=$(sudo docker ps -a | grep -i $1 | wc -l ) 
     if [ ${containerExist} -gt 0 ]
      then
      pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
      if [ "${pid}" != "0" ]
       then 
       echo "${statusLived}"
      else
       echo "${statusdead}"
      fi
     else
      echo "${notExistContainer}" 
     fi
    }
    function StopContainer(){
     sudo docker stop $1
    }
    for containerID in ${containerIDs}
     do
     for ((i=1;i<=${retryCount};i++))
     do
      status=$(GetContainerStatus ${containerID} )
      echo "Container ${containerID} status is ${status}"
      if [ "${status}" == ${statusdead} ]
      then
      echo "Container ${containerID} already stopped"
      break
      fi
      if [ "${status}" == ${notExistContainer} ]
      then
      echo "Container ${containerID} not existed"
      break
      fi
      if [ "${status}" == ${statusLived} ]
      then
       echo "Container ${containerID} is lived ,stop container"
       StopContainer ${containerID}
       verifyStatus=$(GetContainerStatus ${containerID} )
       if [ "${verifyStatus}" == ${statusdead} ]
       then
        echo "stop container ${containerID} success "
        break
       else
       echo "${i} retry stop container"
       StopContainer ${containerID}
       fi
      fi
     done
    done
    
青海十一选五开奖数据 北京11选五前三组 海南环岛赛体彩规律 股票数据导出exc 850棋牌最大的老板 bbin金沙app下载 江苏快3今天走势图i 英超联赛积分榜 海南飞鱼开奖直播网址 如何加入微信群 十一运夺金开奖