isposix

Un outil pour savoir si une commande est posix - retour accueil

git clone git://bebou.netlib.re/isposix
Log | Files | Refs | README |

wait.html (18433B)


      1 <!-- Copyright 2001-2024 IEEE and The Open Group, All Rights Reserved -->
      2 <!DOCTYPE HTML>
      3 <html lang="en">
      4 <head>
      5 <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.8.0">
      6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      7 <link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group rhtm tool v1.2.4 -->
      8 <!-- Copyright (c) 2001-2024 The Open Group, All Rights Reserved -->
      9 <title>wait</title>
     10 </head>
     11 <body bgcolor="white">
     12 <div class="NAVHEADER">
     13 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0">
     14 <tr class="nav">
     15 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/vi.html" accesskey="P">&lt;&lt;&lt;
     16 Previous</a></td>
     17 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td>
     18 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/wc.html" accesskey="N">Next &gt;&gt;&gt;</a></td>
     19 </tr>
     20 </table>
     21 <hr align="left" width="100%"></div>
     22 <script language="JavaScript" src="../jscript/codes.js"></script><basefont size="3">
     23 <center><font size="2">The Open Group Base Specifications Issue 8<br>
     24 IEEE Std 1003.1-2024<br>
     25 Copyright © 2001-2024 The IEEE and The Open Group</font></center>
     26 <hr size="2" noshade>
     27 <a name="top" id="top"></a> <a name="wait" id="wait"></a> <a name="tag_20_147" id="tag_20_147"></a><!-- wait -->
     28 <h4 class="mansect"><a name="tag_20_147_01" id="tag_20_147_01"></a>NAME</h4>
     29 <blockquote>wait — await process completion</blockquote>
     30 <h4 class="mansect"><a name="tag_20_147_02" id="tag_20_147_02"></a>SYNOPSIS</h4>
     31 <blockquote class="synopsis">
     32 <p><code><tt>wait</tt> <b>[</b><i>pid</i><tt>...</tt><b>]</b></code></p>
     33 </blockquote>
     34 <h4 class="mansect"><a name="tag_20_147_03" id="tag_20_147_03"></a>DESCRIPTION</h4>
     35 <blockquote>
     36 <p>The <i>wait</i> utility shall wait for one or more child processes whose process IDs are known in the current shell execution
     37 environment (see <a href="../utilities/V3_chap02.html#tag_19_13"><i>2.13 Shell Execution Environment</i></a> ) to terminate.</p>
     38 <p>If the <i>wait</i> utility is invoked with no operands, it shall wait until all process IDs known to the invoking shell have
     39 terminated and exit with a zero exit status.</p>
     40 <p>If one or more <i>pid</i> operands are specified that represent known process IDs, the <i>wait</i> utility shall wait until all
     41 of them have terminated. If one or more <i>pid</i> operands are specified that represent unknown process IDs, <i>wait</i> shall
     42 treat them as if they were known process IDs that exited with exit status 127. The exit status returned by the <i>wait</i> utility
     43 shall be the exit status of the process requested by the last <i>pid</i> operand.</p>
     44 <p>Once a process ID that is known in the current shell execution environment (see <a href=
     45 "../utilities/V3_chap02.html#tag_19_13"><i>2.13 Shell Execution Environment</i></a> ) has been successfully waited for, it shall be
     46 removed from the list of process IDs that are known in the current shell execution environment. If the process ID is associated
     47 with a background job, the corresponding job shall also be removed from the list of background jobs.</p>
     48 </blockquote>
     49 <h4 class="mansect"><a name="tag_20_147_04" id="tag_20_147_04"></a>OPTIONS</h4>
     50 <blockquote>
     51 <p>None.</p>
     52 </blockquote>
     53 <h4 class="mansect"><a name="tag_20_147_05" id="tag_20_147_05"></a>OPERANDS</h4>
     54 <blockquote>
     55 <p>The following operand shall be supported:</p>
     56 <dl compact>
     57 <dd></dd>
     58 <dt><i>pid</i></dt>
     59 <dd>One of the following:
     60 <ol>
     61 <li>
     62 <p>The unsigned decimal integer process ID of a child process whose termination the utility is to wait for.</p>
     63 </li>
     64 <li>
     65 <p>A job ID (see XBD <a href="../basedefs/V1_chap03.html#tag_03_182"><i>3.182 Job ID</i></a> ) that identifies a process group in
     66 the case of a job-control background job, or a process ID in the case of a non-job-control background job (if supported), to be
     67 waited for. The job ID notation is applicable only for invocations of <i>wait</i> in the current shell execution environment; see
     68 <a href="../utilities/V3_chap02.html#tag_19_13"><i>2.13 Shell Execution Environment</i></a> . The exit status of <i>wait</i> shall
     69 be determined by the exit status of the last pipeline to be executed. <basefont size="2"></p>
     70 <dl>
     71 <dt><b>Note:</b></dt>
     72 <dd>The job ID type of <i>pid</i> is only available on systems supporting the User Portability Utilities option or supporting
     73 non-job-control background jobs.</dd>
     74 </dl>
     75 <basefont size="3"></li>
     76 </ol>
     77 </dd>
     78 </dl>
     79 </blockquote>
     80 <h4 class="mansect"><a name="tag_20_147_06" id="tag_20_147_06"></a>STDIN</h4>
     81 <blockquote>
     82 <p>Not used.</p>
     83 </blockquote>
     84 <h4 class="mansect"><a name="tag_20_147_07" id="tag_20_147_07"></a>INPUT FILES</h4>
     85 <blockquote>
     86 <p>None.</p>
     87 </blockquote>
     88 <h4 class="mansect"><a name="tag_20_147_08" id="tag_20_147_08"></a>ENVIRONMENT VARIABLES</h4>
     89 <blockquote>
     90 <p>The following environment variables shall affect the execution of <i>wait</i>:</p>
     91 <dl compact>
     92 <dd></dd>
     93 <dt><i>LANG</i></dt>
     94 <dd>Provide a default value for the internationalization variables that are unset or null. (See XBD <a href=
     95 "../basedefs/V1_chap08.html#tag_08_02"><i>8.2 Internationalization Variables</i></a> for the precedence of internationalization
     96 variables used to determine the values of locale categories.)</dd>
     97 <dt><i>LC_ALL</i></dt>
     98 <dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
     99 <dt><i>LC_CTYPE</i></dt>
    100 <dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
    101 opposed to multi-byte characters in arguments).</dd>
    102 <dt><i>LC_MESSAGES</i></dt>
    103 <dd><br>
    104 Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.</dd>
    105 <dt><i>NLSPATH</i></dt>
    106 <dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
    107 Determine the location of messages objects and message catalogs. <img src="../images/opt-end.gif" alt="[Option End]" border=
    108 "0"></dd>
    109 </dl>
    110 </blockquote>
    111 <h4 class="mansect"><a name="tag_20_147_09" id="tag_20_147_09"></a>ASYNCHRONOUS EVENTS</h4>
    112 <blockquote>
    113 <p>Default.</p>
    114 </blockquote>
    115 <h4 class="mansect"><a name="tag_20_147_10" id="tag_20_147_10"></a>STDOUT</h4>
    116 <blockquote>
    117 <p>Not used.</p>
    118 </blockquote>
    119 <h4 class="mansect"><a name="tag_20_147_11" id="tag_20_147_11"></a>STDERR</h4>
    120 <blockquote>
    121 <p>The standard error shall be used only for diagnostic messages.</p>
    122 </blockquote>
    123 <h4 class="mansect"><a name="tag_20_147_12" id="tag_20_147_12"></a>OUTPUT FILES</h4>
    124 <blockquote>
    125 <p>None.</p>
    126 </blockquote>
    127 <h4 class="mansect"><a name="tag_20_147_13" id="tag_20_147_13"></a>EXTENDED DESCRIPTION</h4>
    128 <blockquote>
    129 <p>None.</p>
    130 </blockquote>
    131 <h4 class="mansect"><a name="tag_20_147_14" id="tag_20_147_14"></a>EXIT STATUS</h4>
    132 <blockquote>
    133 <p>If one or more operands were specified, all of them have terminated or were not known in the invoking shell execution
    134 environment, and the status of the last operand specified is known, then the exit status of <i>wait</i> shall be the status of the
    135 last operand specified. If the process terminated abnormally due to the receipt of a signal, the exit status shall be greater than
    136 128 and shall be distinct from the exit status generated by other signals, but the exact value is unspecified. (See the <a href=
    137 "../utilities/kill.html"><i>kill</i></a> <b>-l</b> option.) Otherwise, the <i>wait</i> utility shall exit with one of the following
    138 values:</p>
    139 <dl compact>
    140 <dd></dd>
    141 <dt>&nbsp;&nbsp;&nbsp;&nbsp;0</dt>
    142 <dd>The <i>wait</i> utility was invoked with no operands and all process IDs known by the invoking shell have terminated.</dd>
    143 <dt>1-126</dt>
    144 <dd>The <i>wait</i> utility detected an error.</dd>
    145 <dt>&nbsp;&nbsp;127</dt>
    146 <dd>The process ID specified by the last <i>pid</i> operand specified is not known in the invoking shell execution
    147 environment.</dd>
    148 </dl>
    149 </blockquote>
    150 <h4 class="mansect"><a name="tag_20_147_15" id="tag_20_147_15"></a>CONSEQUENCES OF ERRORS</h4>
    151 <blockquote>
    152 <p>Default.</p>
    153 </blockquote>
    154 <hr>
    155 <div class="box"><em>The following sections are informative.</em></div>
    156 <h4 class="mansect"><a name="tag_20_147_16" id="tag_20_147_16"></a>APPLICATION USAGE</h4>
    157 <blockquote>
    158 <p>This utility is required to be intrinsic. See <a href="../utilities/V3_chap01.html#tag_18_07"><i>1.7 Intrinsic Utilities</i></a>
    159 for details.</p>
    160 <p>On most implementations, <i>wait</i> is a shell built-in. If it is called in a subshell or separate utility execution
    161 environment, such as one of the following:</p>
    162 <pre>
    163 <tt>(wait)
    164 nohup wait ...
    165 find . -exec wait ... \;
    166 </tt></pre>
    167 <p>it returns immediately because there are no known process IDs to wait for in those environments.</p>
    168 <p>The use of job ID notation is not dependent on job control being enabled. When job control has been disabled using <a href=
    169 "../utilities/set.html"><i>set</i></a> <b>+m</b>, <i>wait</i> can still be used to wait for the process group associated with a
    170 job-control background job, or the process ID associated with a non-control background job (if supported), using</p>
    171 <pre>
    172 <tt>wait %&lt;</tt><i>background job number</i><tt>&gt;
    173 </tt></pre>
    174 <p>See also the RATIONALE for <a href="../utilities/jobs.html"><i>jobs</i></a> and <a href=
    175 "../utilities/kill.html"><i>kill</i></a>.</p>
    176 <p>The shell is allowed to discard the status of any process if it determines that the application cannot get the process ID for
    177 that process from the shell. It is also required to remember only {CHILD_MAX} number of processes in this way. Since the only way
    178 to get the process ID from the shell is by using the <tt>'!'</tt> shell parameter, the shell is allowed to discard the status of an
    179 asynchronous AND-OR list if <tt>"$!"</tt> was not referenced before another asynchronous AND-OR list was started. (This means that
    180 the shell only has to keep the status of the last asynchronous AND-OR list started if the application did not reference
    181 <tt>"$!"</tt>. If the implementation of the shell is smart enough to determine that a reference to <tt>"$!"</tt> was not saved
    182 anywhere that the application can retrieve it later, it can use this information to trim the list of saved information. Note also
    183 that a successful call to <i>wait</i> with no operands discards the exit status of all asynchronous AND-OR lists.)</p>
    184 <p>If the exit status of <i>wait</i> is greater than 128, there is no way for the application to know if the waited-for process
    185 exited with that value or was killed by a signal. Since most utilities exit with small values, there is seldom any ambiguity. Even
    186 in the ambiguous cases, most applications just need to know that the asynchronous job failed; it does not matter whether it
    187 detected an error and failed or was killed and did not complete its job normally.</p>
    188 <p>Some historical shells returned from wait when a process stops instead of only when it terminates. This standard does not allow
    189 wait to return when a process stops for two reasons:</p>
    190 <ol>
    191 <li>
    192 <p>The vast majority, if not all, shell scripts that use <i>wait</i> (without using an extension) expect it not to return until the
    193 process terminates.</p>
    194 </li>
    195 <li>
    196 <p>It is not possible to write a portable shell script that can correctly handle <i>wait</i> returning when a process stops,
    197 because an exit status indicating a process was stopped by a signal cannot be distinguished from one indicating that the process
    198 called <a href="../functions/exit.html"><i>exit</i>()</a> with the same value.</p>
    199 </li>
    200 </ol>
    201 <p>The standard developers considered allowing interactive shells to return from <i>wait</i> when a process stops, since the
    202 interactive user would see a message which would allow them to tell whether the process stopped or terminated. However, they
    203 decided that it would be inadvisable to introduce an inconsistency between interactive and non-interactive shells, particularly as
    204 the most likely use of <i>wait</i> in an interactive shell is to try out commands before putting them in a shell script.
    205 Implementations can provide an extension that could be used to request that <i>wait</i> returns when a process stops. It is
    206 recommended that any such extension uses a different method of returning information about the wait status of the process so that
    207 the information can be unambiguous. One suitable method would be an option that takes a variable name as an option-argument. The
    208 named variable would be set to a numeric value and the exit status of wait would indicate whether this value is an exit value or a
    209 signal number, and whether the signal terminated the process or stopped it. Such an extension would also provide a way for shell
    210 scripts to obtain the full exit value (as would be returned by <a href="../functions/waitid.html"><i>waitid</i>()</a>).</p>
    211 </blockquote>
    212 <h4 class="mansect"><a name="tag_20_147_17" id="tag_20_147_17"></a>EXAMPLES</h4>
    213 <blockquote>
    214 <p>Although the exact value used when a process is terminated by a signal is unspecified, if it is known that a signal terminated a
    215 process, a script can still reliably determine which signal by using <a href="../utilities/kill.html"><i>kill</i></a> as shown by
    216 the following script:</p>
    217 <pre>
    218 <tt>sleep 1000&
    219 pid=$!
    220 kill -kill $pid
    221 wait $pid
    222 echo $pid was terminated by a SIG$(kill -l $?) signal.
    223 </tt></pre>
    224 <p>If the following sequence of commands is run in less than 31 seconds:</p>
    225 <pre>
    226 <tt>sleep 257 | sleep 31 &
    227 jobs -l %%
    228 </tt></pre>
    229 <p>either of the following commands returns the exit status of the second <a href="../utilities/sleep.html"><i>sleep</i></a> in the
    230 pipeline:</p>
    231 <pre>
    232 <tt>wait </tt><i>&lt;pid of sleep 31&gt;</i><tt>
    233 wait %%
    234 </tt></pre></blockquote>
    235 <h4 class="mansect"><a name="tag_20_147_18" id="tag_20_147_18"></a>RATIONALE</h4>
    236 <blockquote>
    237 <p>The description of <i>wait</i> does not refer to the <a href="../functions/waitpid.html"><i>waitpid</i>()</a> function from the
    238 System Interfaces volume of POSIX.1-2024 because that would needlessly overspecify this interface. However, the wording means that
    239 <i>wait</i> is required to wait for an explicit process when it is given an argument so that the status information of other
    240 processes is not consumed. Historical implementations use the <a href="../functions/wait.html"><i>wait</i>()</a> function defined
    241 in the System Interfaces volume of POSIX.1-2024 until <a href="../functions/wait.html"><i>wait</i>()</a> returns the requested
    242 process ID or finds that the requested process does not exist. Because this means that a shell script could not reliably get the
    243 status of all background children if a second background job was ever started before the first job finished, it is recommended that
    244 the <i>wait</i> utility use a method such as the functionality provided by the <a href=
    245 "../functions/waitpid.html"><i>waitpid</i>()</a> function.</p>
    246 <p>The ability to wait for multiple <i>pid</i> operands was adopted from the KornShell.</p>
    247 <p>This new functionality was added because it is needed to determine the exit status of any asynchronous AND-OR list accurately.
    248 The only compatibility problem that this change creates is for a script like</p>
    249 <pre>
    250 <tt>while sleep 60 do
    251     job& echo Job started $(date) as $!  done
    252 </tt></pre>
    253 <p>which causes the shell to monitor all of the jobs started until the script terminates or runs out of memory. This would not be a
    254 problem if the loop did not reference <tt>"$!"</tt> or if the script would occasionally <i>wait</i> for jobs it started.</p>
    255 </blockquote>
    256 <h4 class="mansect"><a name="tag_20_147_19" id="tag_20_147_19"></a>FUTURE DIRECTIONS</h4>
    257 <blockquote>
    258 <p>A future version of this standard may add an option which takes a variable name as an option-argument, allowing <i>wait</i> to
    259 return information about the wait status of a process in an unambiguous way.</p>
    260 </blockquote>
    261 <h4 class="mansect"><a name="tag_20_147_20" id="tag_20_147_20"></a>SEE ALSO</h4>
    262 <blockquote>
    263 <p><a href="../utilities/V3_chap02.html#tag_19"><i>2. Shell Command Language</i></a> , <a href=
    264 "../utilities/kill.html#tag_20_64"><i>kill</i></a> , <a href="../utilities/sh.html#"><i>sh</i></a></p>
    265 <p>XBD <a href="../basedefs/V1_chap03.html#tag_03_182"><i>3.182 Job ID</i></a> , <a href="../basedefs/V1_chap08.html#tag_08"><i>8.
    266 Environment Variables</i></a></p>
    267 <p>XSH <a href="../functions/wait.html#tag_17_658"><i>wait</i></a></p>
    268 </blockquote>
    269 <h4 class="mansect"><a name="tag_20_147_21" id="tag_20_147_21"></a>CHANGE HISTORY</h4>
    270 <blockquote>
    271 <p>First released in Issue 2.</p>
    272 </blockquote>
    273 <h4 class="mansect"><a name="tag_20_147_22" id="tag_20_147_22"></a>Issue 8</h4>
    274 <blockquote>
    275 <p>Austin Group Defect 854 is applied, adding a note to the APPLICATION USAGE section that this utility is required to be
    276 intrinsic.</p>
    277 <p>Austin Group Defect 1122 is applied, changing the description of <i>NLSPATH .</i></p>
    278 <p>Austin Group Defect 1254 is applied, updating various requirements for the <a href="../utilities/jobs.html"><i>jobs</i></a>
    279 utility to account for the addition of <a href="../utilities/V3_chap02.html#tag_19_11"><i>2.11 Job Control</i></a> .</p>
    280 </blockquote>
    281 <div class="box"><em>End of informative text.</em></div>
    282 <hr>
    283 <p>&nbsp;</p>
    284 <a href="#top"><span class="topOfPage">return to top of page</span></a><br>
    285 <hr size="2" noshade>
    286 <center><font size="2">UNIX® is a registered Trademark of The Open Group.<br>
    287 POSIX™ is a Trademark of The IEEE.<br>
    288 Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved<br>
    289 [ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
    290 "../functions/contents.html">XSH</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../xrat/contents.html">XRAT</a>
    291 ]</font></center>
    292 <hr size="2" noshade>
    293 <div class="NAVHEADER">
    294 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0">
    295 <tr class="nav">
    296 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/vi.html" accesskey="P">&lt;&lt;&lt;
    297 Previous</a></td>
    298 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td>
    299 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/wc.html" accesskey="N">Next &gt;&gt;&gt;</a></td>
    300 </tr>
    301 </table>
    302 <hr align="left" width="100%"></div>
    303 </body>
    304 </html>