The POSIX shell built-in wait causes the shell to wait
until a previously started background process is complete (if you call wait with the required process ID), or
until all previously started background processes terminate (if you call wait without arguments).
When a command is run in the background (with &), the interactive shell does not normally wait for it to finish before prompting you for a new command. By contrast, if a command is started as a foreground process, the shell will always wait for it.
This also applies with respect to shell scripts. Thus, if a script contains a command that is to process the output of a previously started background command, wait can be used to ensure that the background command terminates first.
If the error message
fork failed - too many processes
appears when you try to run a process in the background, you may be able to lessen the system load by running wait. If this has no effect, it may be that the system table is full or that there are too many active foreground processes.
Hint
If a pipeline has 3 or more stages, the shell groups processes into pairs and spawns a process to control each pair. Hence the original processes are not child processes of the shell. wait cannot wait for processes which are not children of the shell.
Syntax
wait[ process_ID...] |
Process identification number of the background process whose completion is to be waited for by the shell. Only one process ID may be specified. If more than one process ID is given in the command line, wait will only consider the first one and will ignore the rest without issuing an error message. The shell always assigns the process ID of the last command run in the background to the ! (exclamation point) variable. The contents of this variable can be accessed with $!. process_ID not specified: |
Exit status
If you have called wait without an argument and all known process IDs have terminated,the exit status is always 0. |
1-126 | Error |
127 | The command for the last process ID is unknown. |
Locale
The following environment variables affect the execution of wait: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE 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 and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
The shell is to wait for an active background process:
|
Example 2
The shell script patience illustrates how to wait for a specific background command to terminate. The contents of this shell script are as follows: : Invocation with sh patience sort list > sorted & pid1=$! tar cvf /dev/dsk/f0t /home/anne & pid2=$! ... wait $pid1 pg sorted The process IDs of the two background command are stored in the variables pid1 and pid2, respectively. These process IDs are thus available for future use, as the ! variable always holds the process ID of the last background command started. The wait command causes the script to wait for the sort command to complete before displaying the contents of sorted with the pg command. |
See also
waitpid() [4] |