Re: Kill sends signal to wrong process group with job control enabled

Ben Ashton <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Message-ID <[email protected]>
On 01/03/2026 22:18, Oğuz wrote:
> On Monday, March 2, 2026, Ben Ashton <[email protected] <mailto:[email protected]>> 
> wrote:
> 
>     As you can see, bash is the only one that translates $! to the PGID
>     of the subshell.
> 
> 
>   I don't think that is what's going on here. It looks more like bash 
> turns off job control in the subshell and includes `setsid sleep 5 &' in 
> the subshell's process group, which makes sense.
> 
> 
> -- 
> Oğuz
>

That IS what's going on. Setsid makes sleep the leader of a new process 
group.

Runing this script:

   #!/usr/bin/env bash
   set -m
   (
     setsid sleep 40 &
     pid=$!
     echo "Sleep pid: $pid"
     sleep 20
     kill -TERM -$pid
     echo "Here"
   )

With "strace -f -e 'trace=kill' ./script.sh", gives the following output 
(edited for brevity + comments)

   strace: Process 17584 attached <- Subshell
   strace: Process 17585 attached <- Sleep 40 (after setsid + execvp)
   Sleep pid: 17585 <- sleep PID is the same as its PGID (see below)
   strace: Process 17586 attached <- Sleep 20 (delay before kill)
   [pid 17586] +++ exited with 0 +++ <- Sleep 20 exits
   [pid 17584] --- SIGCHLD {si_pid=17586} --- <- Subshell receives CHLD
   [pid 17584] kill(-17584, SIGTERM)       = 0 <- Kill translates -17585 
to -17584
   [pid 17584] --- SIGTERM {si_pid=17584} --- <- Subshell receives TERM 
from itself
   [pid 17584] +++ killed by SIGTERM +++
   Terminated

Before the kill command is run, here is the process tree:

   PID     PGID
   17581   17581  |  \_ strace -f -e trace=kill ./script.sh
   17583   17581  |      \_ bash ./script.sh
   17584   17584  |          \_ bash ./script.sh
   17585   17585  |              \_ sleep 40
   17586   17584  |              \_ sleep 20
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.