在 php 中杀死进程父进程

Kill process parents in php

我试图通过杀死它来停止进程,但为了避免进程僵尸,我必须在杀死 pid 之前杀死 ppid,这是我杀死 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');

所以,我尝试了这个来获取 ppid,但似乎效果不佳:

$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');

有没有其他方法获取ppid?或者在不留下任何僵尸进程的情况下终止进程?

使用这个,它会起作用

    $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");