Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在proc_打开时安全终止进程_Php_Proc Open - Fatal编程技术网

Php 在proc_打开时安全终止进程

Php 在proc_打开时安全终止进程,php,proc-open,Php,Proc Open,在用proc_open启动PHP内置服务器后,我似乎无法杀死它。 服务器正在工作,但不想关闭进程。我也试过: $status = proc_get_status($this->process); posix_kill($status['pid'], SIGTERM); proc_close($this->process); 我还尝试了SIGINT和SIGSTOP。。。不要使用SIGSTOP 这里使用的是ps,但是我想让它独立于操作系统 完整代码: class SimpleServ

在用proc_open启动PHP内置服务器后,我似乎无法杀死它。

服务器正在工作,但不想关闭进程。我也试过:

$status = proc_get_status($this->process);
posix_kill($status['pid'], SIGTERM);
proc_close($this->process);
我还尝试了
SIGINT
SIGSTOP
。。。不要使用
SIGSTOP

这里使用的是
ps
,但是我想让它独立于操作系统

完整代码:

class SimpleServer
{

    const STDIN = 0;
    const STDOUT = 1;
    const STDERR = 2;

    /**
     * @var resource
     */
    protected $process;

    /**
     * @var []
     */
    protected $pipes;

    /**
     * SimpleAyeAyeServer constructor.
     * @param string $docRoot
     */
    public function __construct($docRoot)
    {
        $docRoot = realpath($docRoot);

        $descriptorSpec = [
            static::STDIN  => ["pipe", "r"],
            static::STDOUT => ["pipe", "w"],
            static::STDERR => ["pipe", "w"],
        ];
        $pipes = [];

        $this->process = proc_open("php -S localhost:8000 -t $docRoot", $descriptorSpec, $pipes);

        // Give it a second and see if it worked
        sleep(1);
        $status = proc_get_status($this->process);
        if(!$status['running']){
            throw new \RuntimeException('Server failed to start: '.stream_get_contents($pipes[static::STDERR]));
        }
    }

    /**
     * Deconstructor
     */
    public function __destruct()
    {
        $status = proc_get_status($this->process);
        posix_kill($status['pid'], SIGSTOP);
        proc_close($this->process);
    }
}
使用

终止由
proc\u open()


proc_terminate($status['pid'],9)

proc\u terminate
是我尝试的第一件事(见上文)。我没有尝试过SIGKILL,但即使这样似乎也没有杀死它。我想知道这是否是PHP内置服务器的一些怪癖。
class SimpleServer
{

    const STDIN = 0;
    const STDOUT = 1;
    const STDERR = 2;

    /**
     * @var resource
     */
    protected $process;

    /**
     * @var []
     */
    protected $pipes;

    /**
     * SimpleAyeAyeServer constructor.
     * @param string $docRoot
     */
    public function __construct($docRoot)
    {
        $docRoot = realpath($docRoot);

        $descriptorSpec = [
            static::STDIN  => ["pipe", "r"],
            static::STDOUT => ["pipe", "w"],
            static::STDERR => ["pipe", "w"],
        ];
        $pipes = [];

        $this->process = proc_open("php -S localhost:8000 -t $docRoot", $descriptorSpec, $pipes);

        // Give it a second and see if it worked
        sleep(1);
        $status = proc_get_status($this->process);
        if(!$status['running']){
            throw new \RuntimeException('Server failed to start: '.stream_get_contents($pipes[static::STDERR]));
        }
    }

    /**
     * Deconstructor
     */
    public function __destruct()
    {
        $status = proc_get_status($this->process);
        posix_kill($status['pid'], SIGSTOP);
        proc_close($this->process);
    }
}