Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: wait for the next process to finish
- X-seq: zsh-users 16632
- From: Rory Mulvaney <rorymulv@xxxxxxxxx>
- To: Rory Mulvaney <rorymulv@xxxxxxxxx>
- Subject: Re: wait for the next process to finish
- Date: Tue, 13 Dec 2011 11:04:44 -0600 (CST)
- Cc: zsh-users@xxxxxxx, Wayne Davison <wayned@xxxxxxxxxxxxxxxxxxxxx>, Anthony R Fletcher <arif@xxxxxxxxxxxx>
- 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=1OzWcmw0zO5DlpVCTCzxvcUkxBlpIUOfWDYNJEpiDNw=; b=ml+wa42zw+Y6rZAzkW9hFMaSODk3ztSkGsReTTrKOU5TcV0QjCf37wlP1R9s0TnP/v 1uxzEt8UtIHXoycQdfLqNMdZhDaxkPl4CdYaJSNVbfprRM7ITNmsS4LPS20nFFwe5Q+S lMfv98NnAXnkbv0XO/ZJaaTtxsO8HLw0VKpHo=
- In-reply-to: <alpine.DEB.2.00.1112130937120.13348@MyComp.localdomain>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20111212154601.GA5198@cosy.cit.nih.gov> <CAHSx_Sv8g+tQQnytijXd=HOSHkyu-vwcmjCaDWoHhp2ba+whHw@mail.gmail.com> <alpine.DEB.2.00.1112130937120.13348@MyComp.localdomain>
On Tue, 13 Dec 2011, Rory Mulvaney wrote:
> On Mon, 12 Dec 2011, Wayne Davison wrote:
>
> > On Mon, Dec 12, 2011 at 7:46 AM, Anthony R Fletcher <arif@xxxxxxxxxxxx>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.
>
Sorry, now I realized that you want to want to wait for whichever
background process finishes first. I know that select (zsh/zselect) is
used for this type of thing, which blocks until some file descriptor is
ready for reading or writing. It would possible but sort of complicated
to hack that functionality into the case of waiting for pids. I can
imagine using coproc to get a file descriptor for each of the background
processes.
Something like this (requiring a zsh version with anonymous functions):
pids=( )
R=( $RANDOM $RANDOM )
echo $R
coproc () { sleep $(( $R[1]/32767.0*20 )) ; echo $$ }
pids+=$!
# copy the coproc fd
exec 3<&p
coproc () { sleep $(( $R[2]/32767.0*20 )) ; echo $$ }
pids+=$!
exec 4<&p
zmodload zsh/zselect
zselect -r 3 4
echo $reply
Then one could parse $reply.
I don't know, maybe there's a much easier way to create file descriptors
for those processes?
-Rory
Messages sorted by:
Reverse Date,
Date,
Thread,
Author