0

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.

Naveen Sharma
  • 1,057
  • 12
  • 32
  • I tried that solution but it doesn't return the pid. even when i output the pid to a file, it doesn't return anything again till i kill the process. – Naveen Sharma Aug 30 '14 at 14:43
  • Why don't you try deamon process. This link may be helpful to you http://kvz.io/blog/2009/01/09/create-daemons-in-php/ – Chand Prakash Aug 31 '14 at 06:59

0 Answers0