Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Waiting for a process without using pid
- X-seq: zsh-users 15405
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Waiting for a process without using pid
- Date: Thu, 16 Sep 2010 07:26:54 -0700
- In-reply-to: <AANLkTinyDC-OoGBzuisMKUg+OqeeqG=HKZrNDuJZL+sD@xxxxxxxxxxxxxx>
- 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: <AANLkTinyDC-OoGBzuisMKUg+OqeeqG=HKZrNDuJZL+sD@xxxxxxxxxxxxxx>
On Sep 16, 4:09pm, Anonymous bin ich wrote:
}
} I am trying to write a 'timeout' script, which will take 2 commands
} and exit after whichever one exits first. Is there a way to do it
} without using pid or polling?
Sure.
job1 &
job2 &
coproc read
trap "trap - CHLD ; kill $! " CHLD # Here $! is PID of coprocess
read -p
This sets up a coprocess child that's blocking on the parent, and then
makes the parent block on the child, creating a deadlock. The trap
breaks the deadlock by killing the coprocess, at which point the
parent wakes up.
You might need to prefix job1 and job2 with "sleep 2 ;" to prevent a
race condition where one of the jobs exits before the trap is ready.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author