gpt4 book ai didi

php - 如何使用 exec() 启动和停止 PHP 开发服务器

转载 作者:行者123 更新时间:2023-12-04 19:22:01 29 4
gpt4 key购买 nike

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

这将停止执行我的脚本:

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

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

最佳答案

这有效:

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

关于php - 如何使用 exec() 启动和停止 PHP 开发服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136275/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com