# Wait (command)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Wait_(command)
> Markdown URL: https://mediated.wiki/source/Wait_(command).md
> Source: https://en.wikipedia.org/wiki/Wait_(command)
> Source revision: 1336333903
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{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](/source/AT%26T_Bell_Laboratories)
| released               = {{Start date and age|1973|11}}
| latest release version = 
| latest release date    = 
| operating system       = [Unix](/source/Unix) and [Unix-like](/source/Unix-like)
| genre                  = [Command](/source/Command_(computing))
| license                = 
| website                = 
}}
In [Unix](/source/Unix) [shells](/source/shell_(computing)), '''<code>wait</code>''' is a [command](/source/command_(computing)) which pauses until execution of a [background process](/source/Background_(computer_software)) has ended.

==Usage==
<syntaxhighlight lang="bash">
 wait [n...]
</syntaxhighlight>

where '''n...''' is a list of [pids](/source/process_identifier) or [job IDs](/source/Job_control_(Unix)) 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](/source/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 <code>wait</code> needs to be aware of the job table of the current shell execution environment.  Under the [POSIX](/source/POSIX) specifications it is required to be a [shell builtin](/source/shell_builtin). <ref>{{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 |archive-date=May 18, 2025 |archive-url=https://web.archive.org/web/20250518160448/https://pubs.opengroup.org/onlinepubs/9799919799/utilities/wait.html |url-status=live }}</ref>

==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](/source/rsync) and simultaneously update the libraries on which this program depends, before building the combination.

<syntaxhighlight lang="bash">
#!/usr/bin/env bash

# Parallel update script which makes use of the wait command

# Update local copy
rsync iona:src/ . &
# Upgrade required libraries, or exit indicating failure if make failed for some reason
make -C lib || exit 1

# Wait for rsync to terminate (may have already happened) and finish the job
wait
make
</syntaxhighlight>

Wait for specified job control id number:
<syntaxhighlight lang="bash">
$ 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
</syntaxhighlight>

==See also==
* [wait (system call)](/source/wait_(system_call))

==References==
{{Reflist}}

==External links==
{{Wikibooks|Guide to Unix|Commands}}
* [https://www.gnu.org/software/bash/manual/bashref.html#index-wait GNU bash reference manual for the <code>wait</code> command]

{{Unix commands}}

Category:Unix SUS2008 utilities
Category:Unix process- and task-management-related software
Category:IBM i Qshell commands

---
Adapted from the Wikipedia article [Wait (command)](https://en.wikipedia.org/wiki/Wait_(command)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Wait_(command)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
