From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19368 invoked by alias); 13 Dec 2011 16:10:13 -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: 16627 Received: (qmail 15107 invoked from network); 13 Dec 2011 16:10:11 -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.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW, T_DKIM_INVALID,T_TO_NO_BRKTS_FREEMAIL autolearn=no version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.161.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version:content-type; bh=FEEjcpkaAyBth44Oxh3w969JLRyAvEBeFG/JsP27bFo=; b=CLRlhS1q+nW3fKj4LAJfYGAYXlaEO17NUzlvogJJsxHwXD87jGNaM6rrI7fO+/KhCu Iykf71o73CXZqZKi5aWVoVLPNG26pM5dp7oCvuaup3YhqWCX5UTepme6MtfjkLQES6wZ 8O3qVcdZvANroI353gFlheANXxN1LQh7R0JjQ= Date: Tue, 13 Dec 2011 10:07:28 -0600 (CST) From: Rory Mulvaney To: zsh-users@zsh.org cc: Wayne Davison , Anthony R Fletcher Subject: Re: wait for the next process to finish In-Reply-To: Message-ID: References: <20111212154601.GA5198@cosy.cit.nih.gov> User-Agent: Alpine 2.00 (DEB 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Mon, 12 Dec 2011, Wayne Davison wrote: > On Mon, Dec 12, 2011 at 7:46 AM, Anthony R Fletcher wrote: > > > How can I wait for just the next job to finish? > > > > One thing that may help you is TRAPCHLD. Sadly, the signal handler doesn't > tell you what pid it is reacting to, nor the exit code. > > TRAPCHLD() { > echo here > oldpids=($pids) > pids=( ) > for p in $oldpids; do > if kill -0 $p 2>/dev/null; then > pids+=$p > else > #wait $p # Sadly, this doesn't work > echo $p exited > fi > done > } > pids=( ) > sleep 10 & > pids+=$! > sleep 20 & > pids+=$! > (sleep 15; false) & > pids+=$! > echo $pids > wait > echo done > > It might be nice to set an environment parameter with the pid and status > info right before the dotrap(SIGCHLD) call in jobs.c. > > ..wayne.. > To clarify (I think this is fairly simple), you can supply the process id as a parameter to 'wait', and though the $! method seems rather clumsy to retrieve the pid (since you have to retrieve it somehow in the next command after spawning the background process), it seems to work mostly in general. So you could do: sleep 20000 & sleep 20 & pid=$! wait $pid That will just wait for the sleep 20 process to complete while the sleep 20000 process still runs in the background. For further reference, I see that $! is documented in sec. 15.5 (Parameters set by the shell) in the zsh texinfo manual. -Rory