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

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



On Fri, Apr 19, 2024 at 12:00 PM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
>
> I don't understand why you are saying that "it should be the process
> group leader for the terminal". This is not true in my case (the
> leader is some ancestor)

I don't understand how you're determining that the leader is an ancestor.

That you have job control at all implies this is an interactive shell.
When zsh starts interactively, it attaches to the TTY and makes itself
the group leader.

Subsequently, each time a foreground job is started, zsh attaches that
to the TTY and makes it a new group leader, only reclaiming the TTY
after the foreground job exits.  If it did not do this, ^Z and ^C
would not work.

The only way for an "ancestor" of the job you want to TERM to be the
group leader is if the job zsh started has subsequently forked again,
in which case zsh doesn't have any knowledge or control of the
"grandchild".

That still doesn't mean that "kill 0" from the zsh process would kill
the desired process group, I didn't actually try it.

> On 2024-04-19 11:30:35 -0700, Bart Schaefer wrote:
> >
> > It's not that $jobstates is not available, it's that the foreground
> > job isn't in the table at all.

I was misled there by trying to use INT.  The current job is reaped
before the trap runs.

> But then, why does "kill %%" work?

It doesn't.

% setopt trapsasync
% trap 'print -raC2 -- ${(kv)jobstates}' TERM
% (sleep 2 ; kill -TERM $$) & sleep 10 & sleep 3 & sleep 5
[1] 40302
[2] 40303
[3] 40305
1  running::40302=running
2  running:-:40303=running
3  running:+:40305=running
4  running::40306=running
[1]    done       ( sleep 2; kill -TERM $$; )
[3]  + done       sleep 3
%
[2]  + done       sleep 10

So in the above, job #4 would be the foreground job, but %+
(equivalent to %%) is job #3.  The foreground job has no % shortcut.
Further evidence:

% (sleep 2 ; kill -TERM $$) &! sleep 10
1  running::40317=running
% trap 'print -- ${jobstates:+SET}' TERM
% (sleep 2 ; kill -TERM $$) &! sleep 10
SET
% trap 'trap -- TERM; kill ${jobstates:+%%}' TERM
% (sleep 2 ; kill -TERM $$) &! sleep 10
kill: no current job
%




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