婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 使用Shell腳本批量啟停Docker服務

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

熱門標簽:外呼系統虛擬號碼 代理外呼系統創業 大連電銷外呼系統運營商 接電話機器人罵人 長春電銷外呼系統代理商 400電話干嘛怎么申請信用卡 400電話申請知乎 百度地圖標注尺寸無法顯示 泰州智能外呼系統排名

最近日常測試中經常需要手動啟動或停止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

Python調用腳本

Python示例腳本如下所示:

import paramiko
def StartContainer(svr,port,user,pwd):
 client = paramiko.SSHClient()
 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 client.connect(svr,port=port, username=user, password=pwd,timeout=5)
 client.exec_command("cd /home/TestCode/ && bash startContainer.sh")
def StopContainer(svr,port,user,pwd):
 client = paramiko.SSHClient()
 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 client.connect(svr, port=port, username=user, password=pwd, timeout=5)
 client.exec_command("cd /home/TestCode/ && bash stopContainer.sh ")

總結

以上所述是小編給大家介紹的使用Shell腳本批量啟停Docker服務,希望對大家有所幫助!

標簽:清遠 雅安 大慶 中衛 臺灣 興安盟 安陽 長治

巨人網絡通訊聲明:本文標題《使用Shell腳本批量啟停Docker服務》,本文關鍵詞  使用,Shell,腳本,批量,啟停,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用Shell腳本批量啟停Docker服務》相關的同類信息!
  • 本頁收集關于使用Shell腳本批量啟停Docker服務的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 靖江市| 张家港市| 固镇县| 朔州市| 苍梧县| 长海县| 赤峰市| 阳山县| 泾阳县| 连江县| 商洛市| 晋城| 玉环县| 小金县| 军事| 天门市| 琼结县| 凌源市| 股票| 五家渠市| 剑阁县| 和顺县| 晋州市| 进贤县| 博野县| 宝山区| 托里县| 邻水| 娱乐| 昌宁县| 宿松县| 开平市| 醴陵市| 陵水| 庆阳市| 鹤峰县| 黎城县| 仁怀市| 荃湾区| 鄂伦春自治旗| 石渠县|