濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 使用Shell腳本批量啟停Docker服務(wù)

使用Shell腳本批量啟停Docker服務(wù)

熱門標(biāo)簽:外呼系統(tǒng)虛擬號(hào)碼 代理外呼系統(tǒng)創(chuàng)業(yè) 大連電銷外呼系統(tǒng)運(yùn)營商 接電話機(jī)器人罵人 長春電銷外呼系統(tǒng)代理商 400電話干嘛怎么申請(qǐng)信用卡 400電話申請(qǐng)知乎 百度地圖標(biāo)注尺寸無法顯示 泰州智能外呼系統(tǒng)排名

最近日常測(cè)試中經(jīng)常需要手動(dòng)啟動(dòng)或停止docker,于是決定寫一個(gè)Shell腳本來代替人工操作,另外該腳本,也可以通過Python腳本實(shí)行遠(yuǎn)程調(diào)用,詳細(xì)如下所示:

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

啟動(dòng)docker

啟動(dòng)腳本詳細(xì)如下所示:

#!/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

停止腳本詳細(xì)如下所示:

#!/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調(diào)用腳本

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 ")

總結(jié)

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

標(biāo)簽:清遠(yuǎn) 雅安 大慶 中衛(wèi) 臺(tái)灣 興安盟 安陽 長治

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Shell腳本批量啟停Docker服務(wù)》,本文關(guān)鍵詞  使用,Shell,腳本,批量,啟停,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用Shell腳本批量啟停Docker服務(wù)》相關(guān)的同類信息!
  • 本頁收集關(guān)于使用Shell腳本批量啟停Docker服務(wù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    汝城县| 武义县| 定安县| 东丽区| 登封市| 鹤庆县| 上思县| 久治县| 瑞丽市| 焦作市| 兰西县| 靖边县| 松滋市| 胶南市| 石林| 罗江县| 故城县| 琼海市| 平顺县| 垦利县| 洪江市| 合水县| 平安县| 河源市| 宜兰县| 文安县| 安图县| 洪泽县| 平湖市| 伊金霍洛旗| 合川市| 德江县| 华蓥市| 资阳市| 池州市| 牡丹江市| 临江市| 顺义区| 会同县| 无极县| 温州市|