1

I run a shell script inside php (using shell_exec). I want to kill it and all processes it created when it is in operation. I know the shell script pid. Do you have any suggestion? I am using ubuntu, php as apache module.

Thank you.

Example:

#!/bin/bash
echo hello
sleep 20

When I kill my script (shell_exec("sudo kill -9 $pid")), the sleep process is not killed which is not desired.

anubhava
  • 761,203
  • 64
  • 569
  • 643

2 Answers2

1

use

pkill -TERM -P pid

will kill the child processes

see this answer

Community
  • 1
  • 1
newkedison
  • 158
  • 1
  • 5
  • it kills the process pid and its children, but not grandchildren or below. – Sang Quang Nguyen Aug 01 '13 at 08:00
  • 1
    [anubhava's answer](http://stackoverflow.com/a/17985574/1032255) below can kill all of them, or you can see other answer in [this question](http://stackoverflow.com/q/392022/1032255), such as [1](http://stackoverflow.com/a/3211182/1032255), [2](http://stackoverflow.com/a/15139734/1032255) – newkedison Aug 02 '13 at 03:00
1

Use this kill command instead:

kill -- -$pid

to kill the running script and all its spawned children.

anubhava
  • 761,203
  • 64
  • 569
  • 643