Ssh

如何讓 ssh 等到命令退出?

  • October 3, 2013

我有一個看起來像這樣的腳本:

SERVER="RHT372 RHTl373 RHT374 RHTl375"

case $1 in
"stop")
 for SERVER in $SERVER
 do
   echo "================================================"
   echo Stopping $SERVER
   ssh  root@$SERVER /var/tmp/stopServer.sh server1 
 done
;;

如何讓 ssh 等待命令完成然後轉到下一個伺服器並發出相同的命令?

舉個例子,看看這個:

   #!/bin/bash

 (( $# != 1 )) && printf "%s\n" "Please provide one parameter"; exit 1;

   servers=( srv1 srv2 srv3 )

   case $1 in
   "stop")
       for srv in "${servers[@]}"
           do
    hostAlive=$(ping -s 64 "$srv" -c 1 | grep packet | awk '{print $(NF-4)}')

               echo "=========="
               echo "Stopping $srv"
       [[ "$hostAlive" == "0%" ]] && ssh root@$srv '/var/tmp/stopServer.sh server1'
           done;;
   "start")
       for srv in "${servers[@]}"
           do
               echo "=========="
               echo "Starting $srv"
               ssh....
           done;;
   *)
       echo "Not a valid option"
       exit 1;;
   esac

ssh 命令只有在前一個命令完成操作後才會執行。如果您需要了解更多資訊,請參閱wait命令。

引用自:https://serverfault.com/questions/543545