https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100203
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This seems to be a bug in the Bash 'kill' builtin.
#!/bin/bash
# pass /usr/bin/kill as $1 to use the command not the bash builtin
kill=${1:-kill}
sleep 60 &
pid1=$!
sleep 60 &
pid2=$!
sh -c "exec > /dev/stdout 2>&1 && ($kill -15 -$pid1 $pid2 || { echo kill pgrp
failed, trying again... ; $kill -15 $pid1 $pid2 || echo $?;})"
ps -ef | awk 'NR==1 || /[s]leep/ {print}'
kill $pid1 $pid2 2>/dev/null
Run without arguments, this script prints something like:
sh: line 0: kill: (-2977556) - No such process
UID PID PPID C STIME TTY TIME CMD
jwakely 2977556 2977555 0 12:14 pts/2 00:00:00 sleep 60
i.e. the first kill command kills $pid2 but fails to kill -$pid1 (because there
is no such process group) but it exits with status 0, so we don't try again
just $pid1 instead of -$pid1
Passing /usr/bin/kill as $1 to the script we get:
kill: sending signal to -2977547 failed: No such process
kill pgrp failed, trying again...
kill: sending signal to 2977548 failed: No such process
0
UID PID PPID C STIME TTY TIME CMD
i.e. the first kill command kills $pid2 and fails to kill -$pid1 (as before),
but because it was only partial success it exits with non-zero status, and we
try again using $pid1 instead of -$pid1. That second command succeeds in
killing $pid1 this time, but gives and error for $pid2 (because it was already
killed).
POSIX says:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/kill.html
The following exit values shall be returned:
0
At least one matching process was found for each pid operand, and the
specified signal was successfully processed for at least one matching process.
>0
An error occurred.
The Bash builtin seems to be wrong here, because no matching process was found
for -$pid1 so it should not return 0.