I'm trying to run multiple background processes and then exit with an error if any of them fail. For whatever reason, I cannot get the exit status from wait to behave as documented. This is maddening because if I try wait manually from the command line, I get the expected results, but when I try to run my script which launches remote processes, wait always succeeds.
#!/bin/bash -ex # ... pids= for remote in $REMOTE; do ssh $remote run-script.sh & pids=${pids:+${pids} }$! done set +e wait $pids result=$? set -e echo $result # should be nonzero because remote script exits with 1 debug output (the exit 1 is from the remote script)
++ pids= ++ for remote in '$REMOTE' ++ pids=8142 ++ set +e ++ wait 8142 ++ ssh user@host run-script.sh ++ exit 1 ++ result=0 ++ set -e ++ echo 0 0 I've also tried using jobs but abandoned it because of inconsistent results if the script ends quickly:
wait < <(jobs -p)
waitreturns the status of the last job that exited. It doesn't always succeed, only if the last job succeeds.