]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man1p/wait.1p
Import of man-pages 1.70
[thirdparty/man-pages.git] / man1p / wait.1p
1 .\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
2 .TH "WAIT" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
3 .\" wait
4 .SH NAME
5 wait \- await process completion
6 .SH SYNOPSIS
7 .LP
8 \fBwait\fP \fB[\fP\fIpid\fP\fB...\fP\fB]\fP
9 .SH DESCRIPTION
10 .LP
11 When an asynchronous list (see \fIAsynchronous Lists\fP ) is started
12 by the
13 shell, the process ID of the last command in each element of the asynchronous
14 list shall become known in the current shell
15 execution environment; see \fIShell Execution Environment\fP .
16 .LP
17 If the \fIwait\fP utility is invoked with no operands, it shall wait
18 until all process IDs known to the invoking shell have
19 terminated and exit with a zero exit status.
20 .LP
21 If one or more \fIpid\fP operands are specified that represent known
22 process IDs, the \fIwait\fP utility shall wait until all
23 of them have terminated. If one or more \fIpid\fP operands are specified
24 that represent unknown process IDs, \fIwait\fP shall
25 treat them as if they were known process IDs that exited with exit
26 status 127. The exit status returned by the \fIwait\fP utility
27 shall be the exit status of the process requested by the last \fIpid\fP
28 operand.
29 .LP
30 The known process IDs are applicable only for invocations of \fIwait\fP
31 in the current shell execution environment.
32 .SH OPTIONS
33 .LP
34 None.
35 .SH OPERANDS
36 .LP
37 The following operand shall be supported:
38 .TP 7
39 \fIpid\fP
40 One of the following:
41 .RS
42 .IP " 1." 4
43 The unsigned decimal integer process ID of a command, for which the
44 utility is to wait for the termination.
45 .LP
46 .IP " 2." 4
47 A job control job ID (see the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
48 Section 3.203, Job Control Job ID) that identifies a background process
49 group to be
50 waited for. The job control job ID notation is applicable only for
51 invocations of \fIwait\fP in the current shell execution
52 environment; see \fIShell Execution Environment\fP . The exit status
53 of \fIwait\fP shall
54 be determined by the last command in the pipeline.
55 .TP 7
56 \fBNote:\fP
57 .RS
58 The job control job ID type of \fIpid\fP is only available on systems
59 supporting the User Portability Utilities option.
60 .RE
61 .sp
62 .LP
63 .RE
64 .sp
65 .SH STDIN
66 .LP
67 Not used.
68 .SH INPUT FILES
69 .LP
70 None.
71 .SH ENVIRONMENT VARIABLES
72 .LP
73 The following environment variables shall affect the execution of
74 \fIwait\fP:
75 .TP 7
76 \fILANG\fP
77 Provide a default value for the internationalization variables that
78 are unset or null. (See the Base Definitions volume of
79 IEEE\ Std\ 1003.1-2001, Section 8.2, Internationalization Variables
80 for
81 the precedence of internationalization variables used to determine
82 the values of locale categories.)
83 .TP 7
84 \fILC_ALL\fP
85 If set to a non-empty string value, override the values of all the
86 other internationalization variables.
87 .TP 7
88 \fILC_CTYPE\fP
89 Determine the locale for the interpretation of sequences of bytes
90 of text data as characters (for example, single-byte as
91 opposed to multi-byte characters in arguments).
92 .TP 7
93 \fILC_MESSAGES\fP
94 Determine the locale that should be used to affect the format and
95 contents of diagnostic messages written to standard
96 error.
97 .TP 7
98 \fINLSPATH\fP
99 Determine the location of message catalogs for the processing of \fILC_MESSAGES
100 \&.\fP
101 .sp
102 .SH ASYNCHRONOUS EVENTS
103 .LP
104 Default.
105 .SH STDOUT
106 .LP
107 Not used.
108 .SH STDERR
109 .LP
110 The standard error shall be used only for diagnostic messages.
111 .SH OUTPUT FILES
112 .LP
113 None.
114 .SH EXTENDED DESCRIPTION
115 .LP
116 None.
117 .SH EXIT STATUS
118 .LP
119 If one or more operands were specified, all of them have terminated
120 or were not known by the invoking shell, and the status of
121 the last operand specified is known, then the exit status of \fIwait\fP
122 shall be the exit status information of the command
123 indicated by the last operand specified. If the process terminated
124 abnormally due to the receipt of a signal, the exit status shall
125 be greater than 128 and shall be distinct from the exit status generated
126 by other signals, but the exact value is unspecified. (See
127 the \fIkill\fP \fB-l\fP option.) Otherwise, the \fIwait\fP utility
128 shall exit with one of
129 the following values:
130 .TP 7
131 \ \ \ \ 0
132 The \fIwait\fP utility was invoked with no operands and all process
133 IDs known by the invoking shell have terminated.
134 .TP 7
135 1-126
136 The \fIwait\fP utility detected an error.
137 .TP 7
138 \ \ 127
139 The command identified by the last \fIpid\fP operand specified is
140 unknown.
141 .sp
142 .SH CONSEQUENCES OF ERRORS
143 .LP
144 Default.
145 .LP
146 \fIThe following sections are informative.\fP
147 .SH APPLICATION USAGE
148 .LP
149 On most implementations, \fIwait\fP is a shell built-in. If it is
150 called in a subshell or separate utility execution
151 environment, such as one of the following:
152 .sp
153 .RS
154 .nf
155
156 \fB(wait)
157 nohup wait ...
158 find . -exec wait ... \\;
159 \fP
160 .fi
161 .RE
162 .LP
163 it returns immediately because there are no known process IDs to wait
164 for in those environments.
165 .LP
166 Historical implementations of interactive shells have discarded the
167 exit status of terminated background processes before each
168 shell prompt. Therefore, the status of background processes was usually
169 lost unless it terminated while \fIwait\fP was waiting for
170 it. This could be a serious problem when a job that was expected to
171 run for a long time actually terminated quickly with a syntax
172 or initialization error because the exit status returned was usually
173 zero if the requested process ID was not found. This volume of
174 IEEE\ Std\ 1003.1-2001 requires the implementation to keep the status
175 of terminated jobs available until the status is
176 requested, so that scripts like:
177 .sp
178 .RS
179 .nf
180
181 \fBj1&
182 p1=$!
183 j2&
184 wait $p1
185 echo Job 1 exited with status $?
186 wait $!
187 echo Job 2 exited with status $?
188 \fP
189 .fi
190 .RE
191 .LP
192 work without losing status on any of the jobs. The shell is allowed
193 to discard the status of any process if it determines that
194 the application cannot get the process ID for that process from the
195 shell. It is also required to remember only {CHILD_MAX} number
196 of processes in this way. Since the only way to get the process ID
197 from the shell is by using the \fB'!'\fP shell parameter, the
198 shell is allowed to discard the status of an asynchronous list if
199 \fB"$!"\fP was not referenced before another asynchronous list
200 was started. (This means that the shell only has to keep the status
201 of the last asynchronous list started if the application did
202 not reference \fB"$!"\fP . If the implementation of the shell is smart
203 enough to determine that a reference to \fB"$!"\fP was
204 not saved anywhere that the application can retrieve it later, it
205 can use this information to trim the list of saved information.
206 Note also that a successful call to \fIwait\fP with no operands discards
207 the exit status of all asynchronous lists.)
208 .LP
209 If the exit status of \fIwait\fP is greater than 128, there is no
210 way for the application to know if the waited-for process
211 exited with that value or was killed by a signal. Since most utilities
212 exit with small values, there is seldom any ambiguity. Even
213 in the ambiguous cases, most applications just need to know that the
214 asynchronous job failed; it does not matter whether it
215 detected an error and failed or was killed and did not complete its
216 job normally.
217 .SH EXAMPLES
218 .LP
219 Although the exact value used when a process is terminated by a signal
220 is unspecified, if it is known that a signal terminated a
221 process, a script can still reliably determine which signal by using
222 \fIkill\fP as shown by
223 the following script:
224 .sp
225 .RS
226 .nf
227
228 \fBsleep 1000&
229 pid=$!
230 kill -kill $pid
231 wait $pid
232 echo $pid was terminated by a SIG$(kill -l $?) signal.
233 \fP
234 .fi
235 .RE
236 .LP
237 If the following sequence of commands is run in less than 31 seconds:
238 .sp
239 .RS
240 .nf
241
242 \fBsleep 257 | sleep 31 &
243 jobs -l %%
244 \fP
245 .fi
246 .RE
247 .LP
248 either of the following commands returns the exit status of the second
249 \fIsleep\fP in the
250 pipeline:
251 .sp
252 .RS
253 .nf
254
255 \fBwait\fP \fI<pid of sleep 31>\fP\fBwait %%
256 \fP
257 .fi
258 .RE
259 .SH RATIONALE
260 .LP
261 The description of \fIwait\fP does not refer to the \fIwaitpid\fP()
262 function from the
263 System Interfaces volume of IEEE\ Std\ 1003.1-2001 because that would
264 needlessly overspecify this interface. However, the
265 wording means that \fIwait\fP is required to wait for an explicit
266 process when it is given an argument so that the status
267 information of other processes is not consumed. Historical implementations
268 use the \fIwait\fP() function defined in the System Interfaces volume
269 of IEEE\ Std\ 1003.1-2001 until
270 \fIwait\fP() returns the requested process ID or finds that the requested
271 process does not
272 exist. Because this means that a shell script could not reliably get
273 the status of all background children if a second background
274 job was ever started before the first job finished, it is recommended
275 that the \fIwait\fP utility use a method such as the
276 functionality provided by the \fIwaitpid\fP() function.
277 .LP
278 The ability to wait for multiple \fIpid\fP operands was adopted from
279 the KornShell.
280 .LP
281 This new functionality was added because it is needed to determine
282 the exit status of any asynchronous list accurately. The only
283 compatibility problem that this change creates is for a script like
284 .sp
285 .RS
286 .nf
287
288 \fBwhile sleep 60 do
289 job& echo Job started $(date) as $! done
290 \fP
291 .fi
292 .RE
293 .LP
294 which causes the shell to monitor all of the jobs started until the
295 script terminates or runs out of memory. This would not be a
296 problem if the loop did not reference \fB"$!"\fP or if the script
297 would occasionally \fIwait\fP for jobs it started.
298 .SH FUTURE DIRECTIONS
299 .LP
300 None.
301 .SH SEE ALSO
302 .LP
303 \fIShell Command Language\fP , \fIkill\fP() , \fIsh\fP , the System
304 Interfaces volume of IEEE\ Std\ 1003.1-2001, \fIwait\fP(), \fIwaitpid\fP()
305 .SH COPYRIGHT
306 Portions of this text are reprinted and reproduced in electronic form
307 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
308 -- Portable Operating System Interface (POSIX), The Open Group Base
309 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
310 Electrical and Electronics Engineers, Inc and The Open Group. In the
311 event of any discrepancy between this version and the original IEEE and
312 The Open Group Standard, the original IEEE and The Open Group Standard
313 is the referee document. The original Standard can be obtained online at
314 http://www.opengroup.org/unix/online.html .