Skip to content
Latchkey

wait: Usage, Options & Common CI Errors

wait pauses the shell until background jobs complete and surfaces their exit codes.

wait is the missing half of running tasks in parallel with &. Without it, a script races ahead and ignores whether the background jobs actually succeeded - a silent source of false-green CI.

What it does

wait is a shell builtin that suspends execution until the specified background jobs (or all of them) finish, then returns the exit status of the awaited job. It is how you collect results from parallel work started with &.

Common usage

Terminal
task_a & task_b &              # start two in parallel
wait                           # wait for all background jobs
build_x & pid=$!
wait "$pid"; echo "x exited $?"
# fail the script if any parallel job failed:
for p in $pids; do wait "$p" || exit 1; done

Options

FormWhat it does
waitWait for all background jobs
wait <pid>Wait for one job; return its exit status
wait -nReturn when the next job finishes (bash)
$!PID of the most recent background job

Common errors in CI

The silent failure: starting jobs with & and not checking wait means a crashed background task is ignored and the step goes green. Capture each PID ($!) and wait "$pid" per job, exiting non-zero on the first failure. Plain wait returns the LAST job's status, masking earlier failures - loop over PIDs instead. wait -n is bash-only. A wait on a PID that is not a child of this shell errors with "not a child of this shell".

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →