The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


wait - await process completion


wait [pid...]


When an asynchronous list (see Lists ) is started by the shell, the process ID of the last command in each element of the asynchronous list becomes known in the current shell execution environment; see Shell Execution Environment .

If the wait utility is invoked with no operands, it will wait until all process IDs known to the invoking shell have terminated and exit with a zero exit status.

If one or more pid operands are specified that represent known process IDs, the wait utility will wait until all of them have terminated. If one or more pid operands are specified that represent unknown process IDs, wait will treat them as if they were known process IDs that exited with exit status 127. The exit status returned by the wait utility will be the exit status of the process requested by the last pid operand.

The known process IDs are applicable only for invocations of wait in the current shell execution environment.




The following operand is supported:
One of the following:
  1. The unsigned decimal integer process ID of a command, for which the utility is to wait for the termination.
  2. A job control job ID (see the XBD specification, Glossary  ) that identifies a background process group to be waited for. The job control job ID notation is applicable only for invocations of wait in the current shell execution environment; see Shell Execution Environment . The exit status of wait is determined by the last command in the pipeline.
    The job control job ID type of pid is available on systems supporting both the job control option and the User Portability Utilities Option, which applies to all XSI-conformant systems.


Not used.




The following environment variables affect the execution of wait:
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
Determine the location of message catalogues for the processing of LC_MESSAGES .




Not used.


Used only for diagnostic messages.






If one or more operands were specified, all of them have terminated or were not known by the invoking shell, and the status of the last operand specified is known, then the exit status of wait will be the exit status information of the command indicated by the last operand specified. If the process terminated abnormally due to the receipt of a signal, the exit status will be greater than 128 and will be distinct from the exit status generated by other signals, but the exact value is unspecified. (See the kill -l option.) Otherwise, the wait utility will exit with one of the following values:
The wait utility was invoked with no operands and all process IDs known by the invoking shell have terminated.
The wait utility detected an error.
The command identified by the last pid operand specified is unknown.




On most implementations, wait is a shell built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:

nohup wait ...
find . -exec wait ... \;

it will return immediately because there will be no known process IDs to wait for in those environments.

Historical implementations of interactive shells have discarded the exit status of terminated background processes before each shell prompt. Therefore, the status of background processes was usually lost unless it terminated while wait was waiting for it. This could be a serious problem when a job that was expected to run for a long time actually terminated quickly with a syntax or initialisation error because the exit status returned was usually zero if the requested process ID was not found. This specification requires the implementation to keep the status of terminated jobs available until the status is requested, so that scripts like:

wait $p1
echo Job 1 exited with status $?
wait $!
echo Job 2 exited with status $?

will work without losing status on any of the jobs. The shell is allowed to discard the status of any process that it determines the application cannot get the process ID from the shell. It is also required to remember only {CHILD_MAX} number of processes in this way. Since the only way to get the process ID from the shell is by using the "!" shell parameter, the shell is allowed to discard the status of an asynchronous list if $! was not referenced before another asynchronous list was started. (This means that the shell only has to keep the status of the last asynchronous list started if the application did not reference $!. If the implementation of the shell is smart enough to determine that a reference to $! was not saved anywhere that the application can retrieve it later, it can use this information to trim the list of saved information. Note also that a successful call to wait with no operands discards the exit status of all asynchronous lists.)

If the exit status of wait is greater than 128, there is no way for the application to know if the waited-for process exited with that value or was killed by a signal. Since most utilities exit with small values, there is seldom any ambiguity. Even in the ambiguous cases, most applications just need to know that the asynchronous job failed; it does not matter whether it detected an error and failed or was killed and did not complete its job normally.


Although the exact value used when a process is terminated by a signal is unspecified, if it is known that a signal terminated a process, a script can still reliably figure out which signal using kill as shown by the following script:

sleep 1000&
kill -kill $pid
wait $pid
echo $pid was terminated by a SIG$(kill -l $?) signal.

If the following sequence of commands is run in less than 31 seconds:

sleep 257 | sleep 31 &
jobs -l %%

either of the following commands will return the exit status of the second sleep in the pipeline:

wait <pid of sleep 31>
wait %%




sh, the XSH specification description of waitpid().

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]