I've PHP function which starts a shell script in my Linux system when a user presses the start button.
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);
}
I want another PHP function to know when the script ends and show the response to the user.
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')]);
}
I also want to give my user, option to stop script in middle and show the result of half script.
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')]);
}
Problem is while end function is running it doesn't allow stop function(or any other function) to do any processing. Javascript shows response as pending in my browser and sending another request for stop also remain pending until end request is not completed.