0

Say I have a PHP script,

//main.php -> PID = 1002
<?php 
exec('ProcessOne');
exec('ProcessTwo');
//... many other exec calls
?>

The main.php creates many other processes with their own pids which may not be recorded/tracked by the script, but the pid of main.php is known.

My question is: how do I kill all those processes created by main.php(including main.php) by only knowing the pid of main.php which is 1002 in my example?

The script should run in Linux.

Thanks

cache
  • 1,239
  • 3
  • 13
  • 21
  • 1
    How would you kill a process in linux shell? How would you determine child processes? (this question is irrelevant to php) – zerkms Jun 17 '12 at 10:48
  • Thanks for your reply. The reason I described it in such detail is because using exec in php may give processes more relationship than I know: same group id? or something else that I may not know but may be useful to solve the problem. – cache Jun 17 '12 at 11:08

1 Answers1

4
pkill -TERM -P 1002

pkill -P PID gives you all child-proccess, -TERM sends the TERM Signal to all children.

To kill the children of the children:

kill `pstree -p 1002 | sed 's/(/\n(/g' | grep '(' | sed 's/(\(.*\)).*/\1/' | tr "\n" " "`

Modified version from Walking a process tree.

Community
  • 1
  • 1
dav1d
  • 5,917
  • 1
  • 33
  • 52
  • Thanks for your answer, but using pkill -TERM -P 1002 only kills direct children of pid 1002. Assuming the ProcessOne has other children processes running as well, what should we do to kill all of those given pid 1002?Thanks – cache Jun 17 '12 at 11:00