kill a group of processes with negative PID

Does it say "no such PID" or is there an error, - as in does this work?

kill -TERM -- -GPID

Also note, as per (emphasize mine)
man 1:

"[…] When an argument of the form '-n' is given, and it is meant to denote a process group […]"

man 2:

"[…] If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid. […]"

man 3:

"[…] If pid is negative, but not -1, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the absolute value of pid, […]"

As in, not PID but process group ID.


Else perhaps you can have so fun with /proc/[pid]/stat

ppid: awk '{gsub(/\([^)]+\)/,"_"); print $4}' /proc/3955/stat
pgrp: awk '{gsub(/\([^)]+\)/,"_"); print $5}' /proc/3955/stat

pkill -TERM -g PGRP


The error message /bin/kill: -23958: No such process may also be due to the fact that the pid 23958 is no pgid (process group id number)!

This can, for example, be the case if you try to kill a backgrounded shell (or command) in a script mistakenly using $! as a pgid; in a job-control enabled shell, however, $! can be used as a pgid (see: Why child process still alive after parent process was killed in Linux?).

# examples of how to kill a process group
# (using sh -c '...' instead of a shell script)

# kill: -<num>: No such process
sh -c '
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!
'

# in Terminal.app 
# job control is enabled by default
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!


# enable job control
sh -c '
set -m 
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!
'

# make sure pid == pgid
# note that the script gets killed as well
sh -c '
echo pid $$
trap "trap - EXIT; echo pid $$ was killed" EXIT
(sleep 200 & sleep 200 & sleep 200) &
IFS=" " read -r pgid <<EOF
$(ps -p $! -o pgid=)
EOF
sleep 5
/bin/kill -s TERM -${pgid}
sleep 5
echo pid $$ was not killed
'

# use a pseudo-terminal (pty) to avoid killing the script
# note the use of -$$ instead of -$!
sh -c '
echo pid $$
script -q /dev/null sh -c '\''
trap "" HUP
(sleep 200 & sleep 200 & sleep 200) &
sleep 5
/bin/kill -s TERM -$$
'\''
sleep 5
echo pid $$ was not killed
'

You could use the "pkill" or the "killtree" solution, shown on this thread on other of the StackExchange sites :)

https://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes

pkill -TERM -P PARENT_PID