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
When a process is forked, it inherits its PGID from its parent. The PGID changes when a process becomes a process group leader, then its PGID is copied from its PID. From then on, the new child processes it spawns, and their descendants, inherit that PGID (unless they start new process groups of their own).
In a shell with job control, such as most interactive shells, each job is put in its own process group. If you run a shell script, the shell process running the script will be the group leader, and the PGID will equal its PID.
In a shell without job control, such as most shells used to run scripts, commands are run in the shell's process group.
The syntax kill -- -N
kills all the processes in the group with PGID = N. You can't use it with an arbitrary PID, only the PID of a process group leader, since that's the PGID. This is essentially how the shell's
kill %jobid
syntax works -- it internally translates %jobid
to the PGID of the job and sends the signal to that PGID.
There's no simple way to run a script in its own process group from another shell script. See How to set process group of a shell script for some suggestions, though.
Best Answer
The
&
command separator will do this for you. Use it carefully and wisely, but here is a simple way to see process relationships:The
[1] 13369
shows thatsleep
(which has PID 13369), has been put into the background as Job #1.$$
returns to the shell the PID of itself, so we feed that intopstree
to show the process tree with a root of our shell's PID, to show all child processes.