1

I'm trying to stop a process by killing it, but to avoid the process zombies I must kill the ppid before killing the pid, here is my code to kill the pid :

$descriptorspec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "a"));
        $process = proc_open("MyProcessCommand", $descriptorspec, $pipes);
        $status = proc_get_status($process);
        $pid = $status['pid'];//Get the process id

{..}

$descriptorspec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "a"));
        proc_open("exec kill -9 $pid", $descriptorspec, $pipes); // killing the pid
        return new JsonResponse('Process Stopped');

So, I tried this one to get the ppid, but it seems not working well :

$descriptorspec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "a"));
            $process = proc_open("MyProcessCommand", $descriptorspec, $pipes);
            $status = proc_get_status($process);
            $pid = $status['pid'];//Get the process id

{..}

    $descriptorspec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "a"));

$ppid = proc_open("exec ps -o ppid= $pid", $descriptorspec, $pipes);

         proc_open("exec kill -9 $ppid", $descriptorspec, $pipes);
         proc_open("exec kill -9 $pid", $descriptorspec, $pipes); // killing the pid
                return new JsonResponse('Process Stopped');

Is there any other way to get the ppid? or to kill the process without leaving any zombie process?

Masseleria
  • 289
  • 3
  • 15
  • `proc_open()` launches a shell and passes its first argument to the shell to be launched as a new process. The shell waits for the termination of the launched process and exits. Are you sure if you kill the process it becomes a zombie? It is not the regular behaviour of shells to ignore the termination of their children. – axiac Feb 23 '16 at 18:08
  • yes it does, look at this result of `ps axf` : `8217 ? S 0:01 \_ /usr/sbin/httpd 8437 ? Z 0:00 | \_ [php] 8221 ? S 0:01 \_ /usr/sbin/httpd 8338 ? Z 0:00 | \_ [php] 8290 ? S 0:01 \_ /usr/sbin/httpd` – Masseleria Feb 23 '16 at 18:12

1 Answers1

1

use this,it will work

    $ppid=shell_exec("ps -o ppid= $pid");
    $int = (int)$ppid;
    var_dump($int);
    shell_exec("exec kill -9 $ppid");
    usleep(300);
    shell_exec("exec kill -9 $pid");
Devilion
  • 74
  • 8