我的PHP函数在用户按下开始按钮时在我的Linux系统中启动shell脚本。
public function start($id)
{
$test=Test::find($id);
$newFile=storage_path('test/script/'.$test->random_string.'.jmx');
$command = "jmeter -n -t ".$newFile.' -l '.storage_path('test/log/'.$test->random_string).'.log';
$background= " > /dev/null 2>&1 &";
// To allow running test in background
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
exec($command. $background,$arr);
// Do processing here
sleep(30);
}
我想要另一个PHP函数来知道脚本何时结束并向用户显示响应。
public function end($id)
{
$test = Test::find($id);
$command="ps aux | grep $test->random_string | grep -v grep | awk '{ print $2 }'";
exec($command, $arr);
while($arr)
{
$arr=null;
sleep(10);
exec($command, $arr);
}
$test->test_running = 0;
$test->save();
return response()->json(['msg'=>trans('test.end')]);
}
我还想给我的用户,选择在中间停止脚本并显示半脚本的结果。
public function stop($id)
{
$test=Test::find($id);
$arr=array();
$background= " > /dev/null 2>&1 &";
exec("ps aux | grep $test->random_string | grep -v grep | awk '{ print $2 }'", $arr);
shell_exec('kill '. $arr[1] . $background);
$test->test_running=0;
$test->save();
return response()->json(['msg'=>trans('test.stop')]);
}
问题是当终端功能正在运行时,它不允许停止功能(或任何其他功能)进行任何处理。 Javascript在我的浏览器中显示响应为挂起,并且发送另一个停止请求也保持挂起状态,直到结束请求未完成。