Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: wait for the next process to finish



On Dec 13, 10:01am, Peter Stephenson wrote:
} Subject: Re: wait for the next process to finish
}
} On Mon, 12 Dec 2011 10:46:01 -0500
} Anthony R Fletcher <arif@xxxxxxxxxxxx> wrote:
} > I just realised that the 'wait' command will either wait for specified
} > jobs or all jobs and nothing in between. The manual says "If job is not
} > given then all currently active child processes are waited for.".
} > 
} > So 
} > 	sleep 30 &
} > 	sleep 10 &
} > 	sleep 30 &
} > 	sleep 30 &
} > 	wait
} > waits for all the sleeps to finish.
} > 
} > How can I wait for just the next job to finish?
} 
} Certainly the shell internals don't help you here.  There's code to look
} at a specific job, decide if it's still going, and exit when it isn't,
} which is behind the wait builtin with an argument.  There's nothing to
} loop over all jobs, decide what's still going, wait for something to
} happen, then work out what it was and hence if it can stop waiting.

What's "just the next job" mean here?

Wait until any one of the four jobs finishes, or wait until a specific
job (e.g., the first one started, and then the second one, and then
the third, etc.) finishes?

I think there have been some answers on this thread that assume one
meaning and some the other, and therefore are contradictory.  If it's
the first meaning (any job) then Wayne's TRAPCHLD suggestion is the
most helpful, but it's true the trap doesn't get useful information
about which child set it off, and on top of that the handler is not
called until the child is already removed from the job table, so even
if you could figure out which one it was you can't "wait" to get its
exit status (I don't think).

In the realm of ugly hacks, you could run each child as a coprocess
that ends with "print $?".  Stash away the coprocess file descriptor
each time you start one, and then when they're all running, start a
loop polling that list of descriptors with "read -t" until you find
one from which you can read.  What you read will be the exit status.



Messages sorted by: Reverse Date, Date, Thread, Author