Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to propagate SIGTERM to the foreground job, if any?



On Tue, Apr 23, 2024 at 8:17 AM Vincent Lefevre <vincent@xxxxxxxxxx> 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.




Messages sorted by: Reverse Date, Date, Thread, Author