Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

wait Command Guide for Pausing Until Background Jobs Finish

wait pauses a script (or your shell) until background jobs complete — the synchronization primitive for job control.


What is wait, and Why Use It?

The wait command blocks execution until specified background jobs finish (or, with no arguments, until all background jobs of the current shell finish). It is how scripts do the thing every shell can already do interactively: start several jobs with &, then say “now pause until they are all done.” Without wait, a script that launches background work races ahead and exits — often before the work finishes.

Why Learn wait?

  • Scripts need to synchronize: anything launched with & in a script needs wait to be reined back in.
  • Exit codes matter: wait returns a job’s exit status, so a script can tell success from failure.
  • Selective waiting: wait PID lets you wait for one job among many.
  • Completes the toolkit: start with &, list with jobs, resume with bg, finish with wait.

wait is the sentence that ends a background-job story: “and then we all stopped, right there, until the work was done.”


1. Basic Usage: Wait for Everything

# Start two jobs
sleep 30 &
sleep 10 &

# Pause until ALL background jobs finish
wait
echo "everything is done"

With no arguments, wait blocks until every job this shell started with & has finished. This is the default and the most common form in scripts.


2. Wait for a Specific Job

sleep 30 &
sleep 10 &

# Wait for only one of them
wait %2

# Wait for a job by its PID
sleep 30 &
wait $!
  • %N — job number, as shown by jobs.
  • $! — the PID of the most recently started background job.

3. Checking the Exit Status

./good-job.sh &
./bad-job.sh &

wait %1
echo "job 1 exit status: $?"    # 0 = success

wait %2
echo "job 2 exit status: $?"    # non-zero = something failed

wait returns the exit status of the job it waited for. This is the standard pattern for “did my background work actually succeed?” — invaluable in build and deploy scripts.


4. Wait With a Timeout

# Wait at most 60 seconds for the job
wait $!          # blocks forever by itself

# Real-world pattern: bound the wait yourself
timeout 60 ./slow-job.sh

wait has no built-in timeout flag in most shells. The common workaround is to run the job under timeout instead, or to poll with jobs in a loop.


5. wait vs. Polling

# Polling — wasteful and racy
while pgrep -f job.sh > /dev/null; do sleep 1; done

# wait — exact, instant, and free
./job.sh &
wait $!

Polling burns CPU and can miss the finish moment entirely. wait blocks on the kernel’s own job record — it wakes the instant the job exits.


Common Pitfalls

  • wait only knows this shell’s jobs: a process detached with nohup + disown, or started by another shell, is invisible to it.
  • Waiting on a finished job returns immediately: if the job already ended, wait does not remember to report failure — capture $? right after.
  • wait without arguments waits for ALL: if one background job is stuck, the whole script hangs with it.
  • Job numbers reset: %1 in a new shell means nothing; use $! or PIDs when in doubt.

PageWhy It Matters
jobsFind the job numbers and PIDs that wait accepts.
bgResume stopped jobs — the other half of job control.
fgBring a job to the foreground instead of waiting for it.
nohupDetach jobs — which also detaches them from wait.

wait Cheatsheet

CommandWhat It Does
waitWait for all background jobs.
wait %1Wait for job number 1.
wait $!Wait for the last job started.
wait PIDWait for a specific process.
echo $?The exit status of the job wait returned.