From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19705 invoked by alias); 13 Dec 2011 17:11:06 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16631 Received: (qmail 9827 invoked from network); 13 Dec 2011 17:11:04 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <111213091049.ZM3465@torch.brasslantern.com> Date: Tue, 13 Dec 2011 09:10:49 -0800 In-reply-to: <20111213100154.0da5d421@pwslap01u.europe.root.pri> Comments: In reply to Peter Stephenson "Re: wait for the next process to finish" (Dec 13, 10:01am) References: <20111212154601.GA5198@cosy.cit.nih.gov> <20111213100154.0da5d421@pwslap01u.europe.root.pri> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Subject: Re: wait for the next process to finish MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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 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.