wait -- await completion of background processes


wait [ pid ... ]


With no arguments, the wait command waits until all background processes known to the current shell have terminated; wait then exits with a value of 0 (zero). (Background processes are started with an ampersand &.)

If a list of process IDs is supplied, wait attends only those processes, and exits when the last process exits. wait returns the exit value of the process specified by the final pid on the command line. If the process pid was unknown to the shell, the exit value is 127. If the process was killed by any signal, wait returns an exit value greater than 128.

Because the wait(S) system call must be executed in the parent process, the shell itself executes wait, without creating a new process. wait is built into csh(C), ksh(C), and sh(C).

The version of kill built into ksh accepts job IDs as well as process IDs if job control is enabled. See ksh(C) for details. Refer to jobs(C) for details of acceptable job ID formats.

Exit values


You can find out what signal killed a child process using the -l option to kill(C):
   program &		# start the background process
   pid=$!			# get process ID
   wait $pid		# wait for the process to terminate
   if [ $? -gt 128 ]	# check the exit value
   	echo $pid killed by signal SIG$(kill -l $?)


wait can only report abnormal terminations using its exit value.

The shell can track the status of at most {CHILD_MAX} processes.

The shell discards the previous value of the ! shell parameter when a new background process is started.

See also

bg(C), csh(C), fg(C), jobs(C), kill(C), ksh(C), sh(C)

Standards conformance

wait is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003