国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

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

發(fā)布時(shí)間:2023-05-25 09:03 來(lái)源:西部 閱讀:64 作者:西部數碼 欄目: 虛擬主機 歡迎投稿:712375056

服務(wù)器

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

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

啟動(dòng)docker

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

#!/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服務(wù),希望對大家有所幫助!

更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問(wèn)題,請訪(fǎng)問(wèn)特網(wǎng)科技官網(wǎng):wap.friendlycc.com.cn

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

成人性爱视频在线观看| 亚洲日韩中文字幕久热| 亚洲免费人成在线视频观看| 护士的小嫩嫩好紧好爽| 专干老肥熟女视频网站| 日韩人妻无码精品—专区|