LinuxCommandLibrary

wait

Wait for child processes to complete

TLDR

Wait for a process to finish given its process ID (PID) and return its exit status

$ wait [pid]
copy

Wait for all processes known to the invoking shell to finish
$ wait
copy

Wait for a job to finish (run jobs to find the job number)
$ wait %[job_number]
copy

Display help
$ wait --help
copy

SYNOPSIS


wait [PID...]
wait -n
wait -f

PARAMETERS

PID...
    One or more process IDs. The command waits for these specific background processes to complete. If a PID refers to a process not known to the current shell as a child, its behavior can be shell-dependent.

-n
    (Bash 5.2+) Waits for a single job to complete and returns its exit status. Useful when you need to confirm at least one background process has finished.

-f
    (Bash 5.2+) Waits for each PID specified to complete, then returns the exit status of the last one. If no PIDs are given, it behaves like wait without options, waiting for all current background jobs.

DESCRIPTION

The wait command is a shell built-in that pauses the execution of the current shell script or interactive session until one or more specified background processes (jobs) have terminated. If no process IDs (PIDs) are provided, wait waits for all currently active background jobs launched from the current shell to complete. When a PID is specified, wait specifically waits for that process. It is commonly used in shell scripting to synchronize the execution flow, ensuring that necessary background tasks finish before subsequent commands are executed. The command's exit status is that of the last process waited for, or 127 if the PID does not exist or an invalid option/PID is given. This allows scripts to check the success or failure of background operations.

CAVEATS

  • Shell Built-in: wait is typically a shell built-in command (e.g., in Bash, Zsh, Dash) and not an external executable. Its behavior can vary slightly between different shells.
  • Job Control: It primarily interacts with jobs launched from the current shell. It might not be able to wait for arbitrary PIDs unless they are child processes of the current shell or if the shell's job control system has a record of them.
  • Exit Status: When multiple PIDs are given without -f (Bash 5.2+), the exit status returned is usually that of the last PID in the list that was waited for, or the last job that terminated if no PIDs are given.
  • Bash 5.2+ Options: The -n and -f options are relatively new, introduced in Bash version 5.2. Scripts relying on these options might not be portable to older Bash versions or other shells.

RETURN VALUE

The exit status of wait is the exit status of the last process for which it waited. If the process exited normally, this is its exit status. If the process terminated due to a signal, the exit status is 128 plus the signal number. If no PIDs are given and no jobs are active, or if an invalid PID is provided, the return status is typically 127.

BLOCKING BEHAVIOR

wait is a blocking command. It will halt the script's execution until the specified conditions (processes terminating) are met. This is crucial for maintaining execution order in scripts involving parallel tasks.

HISTORY

The wait command has been a fundamental part of Unix shells since their early development, providing essential job control capabilities. Its core function of waiting for child processes is inherent to how shells manage background tasks. While its basic functionality has remained consistent, specific options like -n and -f were later additions, particularly in more recent versions of Bash (e.g., Bash 5.2), to provide more granular control over waiting behavior, reflecting evolving scripting needs for concurrent process management.

SEE ALSO

jobs(1), fg(1), bg(1), kill(1)

Copied to clipboard