一部のサーバーを起動するには、次の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
これが非対話型シェルで期待どおりに機能するかどうかはわかりません。