2016-03-24 24 views
2

proc_openでPHPの組み込みサーバーを起動した後、私はそれを強制終了できないようです。 proc_openで安全にプロセスを終了する

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

サーバーが動作している、しかし、それはプロセスをクローズする必要はありません。私も試してみた:

$status = proc_get_status($this->process); 
posix_kill($status['pid'], SIGTERM); 
proc_close($this->process); 

私もSIGSTOPを使用していない... SIGINTSIGSTOPを試してみました。

is a solutionpsを使用していますが、と同じです。

全コード:

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); 
    } 
} 

答えて

0

使用proc_terminate

(参照proc_open()

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

+1

'proc_terminate'は、私が試した最初のものだっによって開始されたプロセスを殺すために適切な機能であります上記)。私は 'SIGKILL'を試みなかったが、それでもそれを殺すようには見えない。私はそれがサーバーに組み込まれたPHPのいくつかの変わったものかどうか疑問に思います。 – DanielM

関連する問題