Shell指令碼批量啟停Docker

2021-10-14 14:14:34 字數 3221 閱讀 9754

目錄

最近日常測試中經常需要手動啟動或停止docker,於是決定寫乙個shell指令碼來代替人工操作,另外該指令碼,也可以通過python指令碼實行遠端呼叫,詳細如下所示:

目前該指令碼是將container id寫死在指令碼中,當然也可以通過傳參給指令碼來進行控制,大家可以改造一下。

啟動指令碼詳細如下所示:

#!/bin/bash

containerids="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"

statuslived="live"

statusdead="dead"

notexistcontainer="none"

retrycount=3

function getcontainerstatus() -gt 0 ]

then

pid=$(sudo docker stats --format "}" --no-stream $1 )

if [ "$" != "0" ]

then

echo "$"

else

echo "$"

fielse

echo "$"

fi}function startcontainer()

for containerid in $

do for((i=1;i<=$;i++))

dostatus=$(getcontainerstatus $ )

echo "container $ status is $"

if [ "$" == $ ]

then

echo "container $ already running"

break

fiif [ "$" == $ ]

then

echo "container $ not existed"

break

fiif [ "$" == $ ]

then

echo "container $ stopped ,start container"

startcontainer $

verifystatus=$(getcontainerstatus $ )

if [ "$" == $ ]

then

echo "start container $ success "

break

else

echo "$ retry start container"

startcontainer $

fifi

done

done

停止指令碼詳細如下所示:

#!/bin/bash

containerids="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"

statuslived="live"

statusdead="dead"

notexistcontainer="none"

retrycount=3

function getcontainerstatus() -gt 0 ]

then

pid=$(sudo docker stats --format "}" --no-stream $1 )

if [ "$" != "0" ]

then

echo "$"

else

echo "$"

fielse

echo "$"

fi}function stopcontainer()

for containerid in $

do for ((i=1;i<=$;i++))

dostatus=$(getcontainerstatus $ )

echo "container $ status is $"

if [ "$" == $ ]

then

echo "container $ already stopped"

break

fiif [ "$" == $ ]

then

echo "container $ not existed"

break

fiif [ "$" == $ ]

then

echo "container $ is lived ,stop container"

stopcontainer $

verifystatus=$(getcontainerstatus $ )

if [ "$" == $ ]

then

echo "stop container $ success "

break

else

echo "$ retry stop container"

stopcontainer $

fifi

done

done

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指令碼 程式啟停

啟動指令碼 bin bash bash c echo processid path to script.sh 指令碼的用途 儲存當前程序號到檔案processid,啟動指令碼 後台執行 bin bash nohup start.sh path to log 指令碼用途 後台執行 start.sh 並...

SVN啟停指令碼

說明 特別注意紅色部分,外部 1傳入msg函式時失效,故特此讀取一遍再傳入!執行時要給指令碼加執行許可權!chmod 755 scripts svn root kazihuo scripts cat svn 1 bin bash 2 by luomurui 2018 05 07 34 f etc i...

nginx啟停指令碼

安裝nginx時,原始碼包中未帶官方的啟動指令碼,也就無法使用service nginxd start這種啟動方式,查了下資料自己寫了乙個 bin bash version 0.0.1 author lifafu file time 2017 02 04 08 00 company source e...