安全地终止proc_open开始的进程
问题描述:
使用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);
我也试过SIGINT
和SIGSTOP
...不要使用SIGSTOP
。
There is a solution using 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_terminate'曾是我的第一件事情(见以上)。我没有尝试'SIGKILL',但即使这似乎并没有杀死它。我想知道是否是服务器内置的PHP的一些怪癖。 – DanielM