help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Possible problem with wait command in bash ?


From: Chet Ramey
Subject: Re: Possible problem with wait command in bash ?
Date: Tue, 25 Aug 2026 16:57:31 -0400
User-agent: Mozilla Thunderbird

On 8/24/26 12:23 AM, Zachary Santer wrote:

I am using the <wait -p finished_pid -n "${list_of_pid[#]}"> command.
If I am not wrong the -n option should only wait on one background job before 
continuing ; and the -p option means that the command will store the PID of the 
finished background job.
I did think that those two options combined would remove only ONE PID at a time

Yep, that's what they should do.

I'm not going to dig through all that right now, but long story short,
try doing 'set -o posix' as the first thing that runs in your script.

That changes several things, but the most significant is when `wait -n' is
supplied without a pid list. See below for details.

We had a discussion about this[1], for like ... months. Chet Ramey,
the bash maintainer, ultimately made some real improvements, but some
of those are only enabled in POSIX mode. You could also try just not
calling the 'jobs' command.

And if I have the values of the 4 variables set to "duration=10" the script 
return that the testing failed (if I look into the debugging files the wait command 
remove more than one job at a time)
Is it normal behavior ??? I thought the -n and -p options together would only remove ONE 
PID at a time from the "jobs table".

At least in certain situations, 'wait -n' won't wait for or report the
termination status of processes that have already terminated when the
wait command is called. (Yes, along the same lines as what our South
Park character was saying, actually.) In particular, if job control or
the 'jobs' command has already notified the user of a process's
termination, 'wait -n' without pid arguments will ignore that process.
I *think* POSIX mode would resolve that in bash 5.3.

I'd have to see some code to make sure this is relevant, but here's a
summary of what posix mode changes in `wait -n' in bash-5.3. I'm only
considering the behavior when given a list of pids, since that's how
the OP is using it.

(At the risk of diving back into this ... quagmire.)

One basic principle is that bash saves a set of exit statuses for
background jobs, even after they terminate (let's leave the `jobs'
command aside for a minute).

The difference between posix mode and default mode in bash-5.3 is that in
posix mode you can only wait for a given terminated process once. If it's
running, `wait' will wait for it, but discard the status upon returning it.
If it's already terminated, you can still retrieve the status, but only once.

In default mode, you can retrieve the status more than one time until you
call `wait' with no arguments or otherwise explicitly discard all the
statuses bash is saving. This doesn't change whether `wait' is called with
-n or without.

So if you want to call `wait -n' with a list of pids or jobs, the burden
is on the programmer to remove a pid from that list when `wait' returns it.
In posix mode, you'll get an error the second time you try to wait for it.
In default mode, you'll keep getting the same status over and over.

The first thing `wait -n' does when given a pid list is to check whether
any pid in that list belongs to any terminated jobs or process
substitutions. It will return one of these, if appropriate, in both posix
mode and default mode. I think that addresses Zach's comment about
`ignoring' that process in this context (see below), but, again, I'd have
to see a reproducer for this.

About the `jobs' command: POSIX says that the shell removes the job when
it reports its termination status, but bash will still allow you to wait
for it (once or multiple times, as above), since `jobs' doesn't really
give you the opportunity to retrieve the exit status. Even if bash
notifies about several jobs during a call to `jobs' and removes them from
the jobs list, you can still use `wait' to retrieve their statuses.
That is, given this script:

{ sleep 2; exit 41 ; } & pid1=$!
{ sleep 2; exit 42 ; } & pid2=$!

sleep 3
jobs

wait -p var -n $pid1
echo var = $var $?

you will get output like

[1]-  Exit 41                    { sleep 2; exit 41; }
[2]+  Exit 42                    { sleep 2; exit 42; }
var = 94252 41

even though POSIX says to discard the status after `jobs'.

One comment about `wait -n' without a pid list: if the shell doesn't
find any terminated jobs in the job list that it hasn't notified the
user about, but there is at least one job that has terminated and for
which bash has saved the status, posix mode will return one of those
saved pid/status pairs and discard it. Default mode doesn't do that,
because it doesn't discard the saved statuses, and there's no way to
prevent it from returning the same pid over and over without discarding.
This is probably what Zach means with his comment about `wait -n' without
pid arguments, but it isn't the OP's use case.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]