Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: kill the LHS command of a pipe once the RHS command terminates
- X-seq: zsh-users 23996
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: kill the LHS command of a pipe once the RHS command terminates
- Date: Sat, 29 Jun 2019 09:17:13 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Gs9ScdnjHMlFqbmZWaa03Oc8MdHD+N+Cr4YSvpTBR60=; b=DJexoCpF0so6c+PgcehUR6gjCl0xQg4k9Xi292t+xV4eKVOZH/2CPHXkc8Y6UI+Toz VWNpENujh8wTjlrSdrOa6Wl+8RC/b88MAMSelOMMbWvk04+5Iun+hnrnVbCmdN1G+dOo bJ0llHjkt1/bAEbjrUE4jIRqzJGh9GuOirL1VgjonmJ5s1tuiT7c9FPnwPG2ZpyYX/in W9aVVP5F94cn65PeeEgUaomUDnhBQlO+8NDvj1ewuXy7fGUtfhwvRZePNwLWMBwdItw0 x6Or0+PQS1vIJhO9X3NqARoMgBLDflxr9Fms6dAeoOAipvBCSdao2doY9IDXo5VmZt4a Uf7w==
- In-reply-to: <20190628110430.GA13790@zira.vinc17.org>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20190628110430.GA13790@zira.vinc17.org>
On Fri, Jun 28, 2019 at 4:05 AM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
>
> With some commands I pipe the output to a pager, and when quitting
> the pager, I want the command to terminate immediately (so that it
> doesn't consume more resources) and want the shell prompt back.
This is going to happen in cases where the left-side command doesn't
produce enough output to fill the OS buffer used for the pipe, so it
doesn't see the end-of-file or receive the PIPE signal when the
right-side command closes the other end. In your examples, you've
also got compound structures that are actually ignoring the state of
their standard output once the small amount of initial output has been
sent. Neither the shell nor the OS has any way of "knowing" that you
didn't INTEND those commands to continue running.
> Does zsh provide a feature to kill the left-hand-side command in a
> clean way (without a race condition, without a temporary file...)?
That's supposed to be the EOF+SIGPIPE, but it only works for "well
behaved" commands that are actually doing something useful with their
standard output.
In addition to Stephane's suggestions, you can also get control of
your left-side by using "coproc". This creates an actual job table
entry that you can manipulate.
zsh% coproc { echo foo; sleep 3; echo err >&2 }; head -n 1 <&p; kill %+
[1] 53747
foo
zsh%
[1] + terminated { echo foo; sleep 3; echo err >&2; }
zsh%
If you're going to kill the right-side with an interrupt or other
signal, you'll have to arrange to trap that so that the subsequent
kill isn't also skipped. Also in practice you want something more
specific than "kill %+" which could kill the wrong job if you have
multiple backgrounded jobs and the coproc exits before the kill
occurs.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author