A script launches 8 uploads in the background with `&` and then calls `wait`. Three uploads failed, but the script exits 0.
IntermediateTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → wait with no arguments waits for all children and returns 0, regardless of their exit statuses. set -e does not apply to background jobs.
Diagnose →
bash -c 'false & true & wait; echo "rc=$?"' # rc=0
Fix → Record each PID and wait for them individually, counting failures:
pids=()
for f in "${files[@]}"; do
upload "$f" & pids+=("$!")
done
failed=0
for pid in "${pids[@]}"; do
wait "$pid" || failed=$((failed + 1))
done
(( failed == 0 )) || die "$failed of ${#pids[@]} uploads failed"
For bounded parallelism with proper exit codes, xargs -0 -P4 -n1 ./upload.sh returns 123 if any invocation failed.
Prevent → Never use a bare wait when the outcome matters; wait -n (Bash 4.3+) is useful for "fail fast on first error".
More on Troubleshooting: strict mode, exit codes, pipelines and traps
A script with `set -e` stops silently right after `((retries++))` on its first pass. No error message, exit code 1.Intermediate`notify.sh: line 9: SLACK_WEBHOOK: unbound variable` — but the webhook is meant to be optional.BeginnerOn a CentOS 7 / Amazon Linux 2 host, `deploy.sh "${extra_args[@]}"` fails with `extra_args[@]: unbound variable`. It works on your laptop.Advanced`local release=$(curl -fsS "$API/latest")` fails with a 503, yet the function happily continues and deploys an empty release name.IntermediateA check `procs=$(ps aux | grep -v grep | grep -c '[n]ginx: worker')` kills the script under `set -euo pipefail` whenever nginx is down, before the alert code runs.IntermediateA script that does `first=$(sort -u huge.txt | head -n 1)` exits with status 141 under `pipefail`, but only on large inputs.Advanced