I want to start a process which outputs to a text file. I want to be able to start it and have seperate button to stop it. I have tried using popen, exec, passthru but my understanding is they all wait till the process exits to return data to php? Is there any way i can just start a process and leave it running. All i want returned is the pid. Here is what I have tried so far.
<?php
$data=$_POST['data'];
$args = escapeshellarg($data);
$cmd = "./fw {$args}". " 2&>lg.log & echo $!";
passthru($cmd, $output);
$arr = array("pid"=>$output);
echo json_encode($arr);
/*
$handle = popen('./fw {$args} 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 209600);
echo $read;
pclose($handle);
*/
?>
Solution suggested in the linked question works with this
$cmd = "./fw {$args}". " >lg.log & echo $!";
exec($cmd, $output);
but if i want stderr redirect too and change it to
$cmd = "./fw {$args}". " 2&>lg.log & echo $!";
exec($cmd, $output);
it stops working. Then i get the return value for pid only after I kill the process.