国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

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

2024-07-09 22:41:32
字體:
來源:轉載
供稿:網友

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

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

啟動docker

啟動腳本詳細如下所示:

#!/bin/bashcontainerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"statusLived="live"statusdead="Dead"notExistContainer="None"retryCount=3function 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 donedone

停止docker

停止腳本詳細如下所示:

#!/bin/bashcontainerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"statusLived="live"statusdead="Dead"notExistContainer="None"retryCount=3function 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 donedone
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金门县| 鄱阳县| 攀枝花市| 许昌县| 绥中县| 潼南县| 措美县| 财经| 屯留县| 怀柔区| 高要市| 鹤庆县| 灌南县| 西林县| 奉化市| 信宜市| 唐山市| 台安县| 桃源县| 什邡市| 靖江市| 安徽省| 通山县| 深圳市| 苍溪县| 株洲县| 宁津县| 丰县| 镇巴县| 阿鲁科尔沁旗| 巫山县| 临猗县| 攀枝花市| 南昌市| 璧山县| 柞水县| 清河县| 保亭| 武陟县| 双鸭山市| 黄骅市|