国产探花免费观看_亚洲丰满少妇自慰呻吟_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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荃湾区| 瑞安市| 循化| 阳山县| 当阳市| 司法| 清徐县| 禄劝| 建瓯市| 纳雍县| 社旗县| 麻阳| 平利县| 宁国市| 安远县| 微山县| 珲春市| 甘南县| 虹口区| 龙江县| 通渭县| 麻栗坡县| 轮台县| 齐河县| 镇安县| 太原市| 集安市| 洪湖市| 郓城县| 道真| 普洱| 噶尔县| 旬邑县| 张家口市| 伊金霍洛旗| 嘉定区| 公安县| 芒康县| 宝清县| 雅江县| 新巴尔虎左旗|