zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: How to propagate SIGTERM to the foreground job, if any?
Date: Tue, 23 Apr 2024 10:32:05 -0700	[thread overview]
Message-ID: <CAH+w=7ZcSSWugWzhc8w4=qzKEqdEAxFoZ3GXvFQ1Bfvgu171zQ@mail.gmail.com> (raw)
In-Reply-To: <20240423151717.GA82287@qaa.vinc17.org>

On Tue, Apr 23, 2024 at 8:17 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> I think that I should start to test with something simpler, with
> a command like
>
>   unison -sshcmd ~/scripts/ssh ~/testdir ssh://remote_host/testdir

I would wish for a test with something simpler than unison as the
driver, but ...

> For ~/scripts/ssh, the following works with I type Ctrl-C
> when ssh-add asks for the passphrase:

If you mean running scripts/ssh from an interactive shell, I'd expect
^C to work even without the trap, because the terminal driver will
deliver the INT to ssh-add before (or effectively simultaneously with)
zsh.

> But %1 might not be the right job in more complex cases.
> BTW, I'm wondering why %1 works, but not %%.

As I said before, %% is the same as %+ which is the most recent
background job.  There's no shortcut to the foreground job because zsh
assumes that a foreground process will get job control signals
directly from the terminal driver -- invoking job control from a
signal trap that bypasses the foreground process was never considered
in the design.

> Moreover "term" is output twice. Why?

I believe what's happening is this:  When you "kill %1" the job exits
with status 143, which indicates it received a TERM signal.  When zsh
gets the SIGCHLD and sees the exit status, it interprets this as
meaning it should also handle the TERM signal.

> But these solutions do not work when ssh-add is in a zsh function

I think that's because using a function wrapper causes the function
itself to be come a subshell that has its own copy of the trap.
Within the subshell, there is no $e (copied from $! only in the
parent) so the trap doesn't work.  What happens if you "exec ssh-add"
in the function instead of just calling it?

> On 2024-04-21 17:41:38 -0700, Bart Schaefer wrote:
> > Try this:
> >   trap 'setopt MONITOR HUP NOCHECKJOBS; exit' TERM
>
> This doesn't work at all with the above test.

Hm, unison may be blocking the HUP signal as well.  Try adding
  trap - HUP
to the script to restore default behavior in the children.


      reply	other threads:[~2024-04-23 17:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 17:17 Vincent Lefevre
2024-04-19 18:30 ` Bart Schaefer
2024-04-19 19:00   ` Vincent Lefevre
2024-04-19 20:06     ` Bart Schaefer
2024-04-19 21:32       ` Vincent Lefevre
2024-04-21  4:09         ` Bart Schaefer
2024-04-21 17:29           ` Vincent Lefevre
2024-04-21 17:43             ` Vincent Lefevre
2024-04-21 21:25               ` Bart Schaefer
2024-04-21 23:59                 ` Vincent Lefevre
2024-04-22  0:41                   ` Bart Schaefer
2024-04-23 15:17                     ` Vincent Lefevre
2024-04-23 17:32                       ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7ZcSSWugWzhc8w4=qzKEqdEAxFoZ3GXvFQ1Bfvgu171zQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).