Bash – バックグラウンドプロセス終了の待機

Bash – バックグラウンドプロセス終了の待機

一部のサーバーを起動するには、次のbashスクリプトがあります。

services=(
    account-service
    reminder-service
    activity-service
    socket-service
    chat-service
    web-app
)


for s in "${services[@]}"; do

 (
   set -e;
   cd "$s"
   git pull
   npm start || exit 1 # always fails
 ) &

 sleep 1;

done

wait;

時にはgit pullコマンドが失敗します。しかし、欠陥はログの深いところにあり、必ずしも明確ではありません。サブシェルのコマンドの1つが1で終了した場合、スクリプト全体をどのように中断しますか?

ベストアンサー1

さて、おそらくwait次のように文を変更してみてください。

while true; do
    wait -n || exit 1          # if one of the background jobs failed, abort
    [[ "$(jobs)" ]] && break   # exit this loop if no more jobs
done

完全にテストされていません。jobsこれが非対話型シェルで期待どおりに機能するかどうかはわかりません。

おすすめ記事