tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: sh: wait-ing for an already terminated background job
Date: Mon, 18 May 2026 22:53:18 +0200
From: Edgar Fu�<ef%math.uni-bonn.de@localhost>
Message-ID: <agt8PrbLaBq0jDwr%trav.math.uni-bonn.de@localhost>
| In sh, is it defined that it works to wait for a background job that has
| already terminated the time wait is invoked?
Yes.
| SUS talks about PIDs "known to the invoking shell" and I'm not sure
| if something else than wait-ing can make a PID unknown.
The jobs command can, sometimes. And in an interactive shell, any
background command that exits, and is reported as having finished, is
also removed from the list. But for a non-interactive shell, wait is
in general the only way.
To be strictly POSIX however, if too many (whatever that means) commands
would be in the list, the shell is permitted to simply drop some, and it
is also permitted to ignore any if the value of $! is not referenced after
the bg command is started, before $! alters next. The former might have
made some sense when memory was very limited, but isn't really needed any
more, the latter seems sensible (you can't wait on a specific command/job
if you cannot identify it) except that that was all specified before job
control was added, along with the '%' notation for identifying jobs, and
so referencing $! is no longer the only way to get an identifier.
| Is there a more elegant portable (i.e. not using wait -n) way to background
| several commands and collect their individual rcs than remembering their
| PIDs and which PID corresponds to which command, then looping through the
| PID list, wait for each one in turn and associate the rc with the command
| remembered?
No. That's why the -n option was added (though it is still not in the std).
| Is that defined to work?
Yes, the method you describe should certainly work. If you don't care
which process finished first, just want to wait for all of them (and check
their status) it is adequate.
kre
Home |
Main Index |
Thread Index |
Old Index