Bashスクリプトは、複数のサーバーのプロセスが完了するのを待ちます。

Bashスクリプトは、複数のサーバーのプロセスが完了するのを待ちます。

複数のサーバーにリモートでSSHを介してそのサーバーで実行されているプロセスがあることを確認し、そのプロセスが完了するのを待ちたいと思います。以下のコードを書いて「continue」ステートメントを追加したので、ファイル(ip.txt)の最初のIPだけを確認します。このコードを修正する必要があります。

  while read IP
  do
    ssh ubuntu@$IP "pgrep -f pattern"
    if [ $? -eq 0 ]; then
       echo "Process is running" 
       sleep 10
       continue
    else
       echo "Process is not running"
    fi
  done < ip.txt

ベストアンサー1

IPリストを最初にソートしてから繰り返すことをお勧めします。

mapfile ipaddresses < ip.txt
canary=alive
while [[ "alive" == "$canary" ]]; do
  canary=dead
  for ip in ${ipaddresses[@]}; do
    if ssh ubuntu@$ip "pgrep -f pattern"; then
      echo "Process is running on $ip"
      canary=alive
      sleep 10
      continue
    else
      echo "Process not running on $ip"
    fi
  done
done

それでも4未満のバージョンで停止している場合は、bashコマンドmapfileを次のように置き換えます。

read -r ipaddresses <<< "$( cat ip.txt )"

おすすめ記事