Wait (command)

{{short description|Command which pauses until execution of a background process has ended}}

{{About|the Unix command||Wait (disambiguation){{!}}Wait}}

{{lowercase title}}

{{Infobox software

| name = wait

| logo =

| screenshot =

| screenshot size =

| caption =

| developer = AT&T Bell Laboratories

| released = {{Start date and age|1973|11}}

| latest release version =

| latest release date =

| operating system = Unix and Unix-like

| genre = Command

| license =

| website =

}}

In Unix shells, wait is a command which pauses until execution of a background process has ended.

Usage

wait [n...]

where n... is a list of pids or job IDs of a currently executing background process (job). If no ids are provided, the command waits until all jobs known to the invoking shell have terminated.

wait normally returns the exit status of the last job which terminated. It may also return 127 in the event that n specifies a non-existent job or zero if there were no jobs to wait for.

Because wait needs to be aware of the job table of the current shell execution environment. Under the POSIX specifications it is required to be a shell builtin. {{cite web|title=The Open Group Base Specifications Issue 8 |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/wait.html | year=2024 | access-date=May 14, 2025|publisher=The Open Group}}

Example

This command can be useful where part of a script can execute in parallel to implement a barrier where an upcoming section depends on the successful completion of the preceding sections.

The following example will fetch the src/ directory from a machine named iona using rsync and simultaneously update the libraries on which this program depends, before building the combination.

  1. !/usr/bin/env bash
  1. Parallel update script which makes use of the wait command
  1. Update local copy

rsync iona:src/ . &

  1. Upgrade required libraries, or exit indicating failure if make failed for some reason

make -C lib || exit 1

  1. Wait for rsync to terminate (may have already happened) and finish the job

wait

make

Wait for specified job control id number:

$ ls -R / > /dev/null 2>&1 & # start any long running background process

[2] 1986

$ wait %2 # waits for background job number 2 to terminate, then returns

See also