0

如何使用 exec() 函数启动和停止 PHP 开发服务器?我需要这样做以自动化我的 BDD 测试。

这将停止执行我的脚本:

echo exec('php -S localhost:8000');

所以我需要一种从 PHP 启动服务器并能够继续执行我的测试的方法。然后我还需要一种从 PHP 中阻止它的方法。

4

2 回答 2

1

这有效:

private function _startDevelopmentServer($pidfile)
{
    $cmd = 'cd ../../public && php -S 127.0.0.1:8027 index.php';
    $outputfile = '/dev/null';
    shell_exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
    sleep(1);
}

private function _killDevelopmentServer($pidfile)
{
    if (file_exists($pidfile)) {
        $pids = file($pidfile);
        foreach ($pids as $pid) {
            shell_exec('kill -9 ' . $pid);
        }
        unlink($pidfile);
    }
}
于 2012-10-30T11:22:21.163 回答
0

这是因为您要停止 Apache。它处理停止命令,但此时服务器已停止,因此它无法处理另一个命令,因为服务器不在那里解析它。编写一个停止并重新启动 Apache 的 shell 脚本,然后从您的 PHP 代码中调用它。即使 Apache 已停止,shell 脚本也应继续执行。

我假设您的服务器是 Apache。

于 2012-10-30T09:54:48.883 回答