bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Ben Ashton
Subject: Re: Kill sends signal to wrong process group with job control enabled
Date: Mon, 2 Mar 2026 02:32:32 -0800
User-agent: Mozilla Thunderbird

On 01/03/2026 22:18, Oğuz wrote:
On Monday, March 2, 2026, Ben Ashton <ben@n0m.org <mailto:ben@n0m.org>> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]