The kill
command, when given a PID that is < -1, treats it as a process group ID (PGID), not as a process ID. This is documented in info kill
:
‘PID < -1’ The process group whose identifier is −PID.
If we take your example again:
$ pstree -p 19935 php(19935)───sh(19936)───spawn(19937)─┬─sleep(19938) └─sleep(19939)
The PGID is the PID of the topmost parent process of the process tree, in this case 19935
. However, you tried to kill the processes belonging to the process group with ID 19937
and 19936
, Neither of which are actually process group IDs. The PGID is 19935
.
You can perhaps see this more clearly with ps
. If I run the same commands on my system:
$ php -f ./zombie.php & [2] 12882 $ ps -o pid,ppid,pgid,command | grep -E '[P]GID|[1]2882' PID PPID PGID COMMAND 12882 1133 12882 php -f ./zombie.php 12883 12882 12882 /bin/bash ./spawn 12884 12883 12882 sleep 1d 12885 12883 12882 sleep 1d
In the example above, the PGID of the group is 12882
, so that's what I need to use if I want to kill everything in the group.
When you run the command from the shell directly, the topmost parent process is the PID of the shell script, so you can kill all processes in its tree by running kill -- -PID
:
$ ./spawn &
[3] 14213
terdon@tpad foo $ ps -o pid,ppid,pgid,command | grep -E '[P]GID|[1]4213'
PID PPID PGID COMMAND
14213 1133 14213 /bin/bash ./spawn
14214 14213 14213 sleep 1d
14215 14213 14213 sleep 1d
But that's because the PID of the shell script is the PGID of the group.
manpreet
Best Answer
2 years ago
I have a script spawning two zombies. I can kill the group via
kill -- -
, but when invoked by the PHP interpreter, that won’t work although killing every single process manually will.The script is
and the PHP file just invokes it:
From the shell directly:
... and via PHP:
only killing the PHP parent process will work:
Any ideas on that?