1

quick question really.

Consider the following code:

//__/var/test/cli_test.php__
$x = 0;
while ($x < 9){
  print "loop " . str_pad($x, 3, "0", STR_PAD_LEFT) . "\n";
  sleep(1);
  $x++;
}

if I type php /var/test/cli_test.php in the command line I get 9 interspaced-by-time lines.. i.e. 9 positive outputs, one per second. EG: these arrive one at a time, blip blip blip...

loop 000 
loop 001 
loop 002 
loop 003 
loop 004 
loop 005 
loop 006 
loop 007 
loop 008 

now consider a different proposition

//__/var/test/cli_test_shell.php
print shell_exec("php /var/test/cli_test.php");

if I type php /var/test/cli_test_shell.php in the command line I get nothing for 9 seconds then everything arrives.. i.e. 1 BIG output 1 BIG wait. after 9 seconds of nothing EG: wait, wait wait.. nothing THEN DUMP:

loop 000 
loop 001 
loop 002 
loop 003 
loop 004 
loop 005 
loop 006 
loop 007 
loop 008 

how can I alter /var/test/cli_test_shell.php so that the output returns each line on per second

0

2 Answers 2

1

try this:

$handle = popen('php /var/test/cli_test_shell.php 2>&1', 'r');
while (!feof($handle)) {
  echo fread($handle, 8192);
}
fclose($handle);
Sign up to request clarification or add additional context in comments.

2 Comments

I fixed it with this disable_ob() stackoverflow.com/questions/1281140/…
I'm glad you found a solution ;)
1

The desired behaviour is not possible using shell_exec(). This is because the function collects the output of the command and returns it as a string - after the command's termination. Use passthru() instead and keep cli_test.php as it is.

4 Comments

thanks :) but you have mis-read, the question was "how can I alter /var/test/cli_test_shell.php" .. you may notice I already "tagged" passthru(), I am not convinced it will function correctly that way without ob_flush's() you see
how should it work with shell_exec()? print will being executed after shell_exec() returns.
shell_exec / exec / passthru it's all one of the same problem, the question is how do I alter it to get 9 piped lines "live", my best guess is that it is a exec with a ob_flush call back on the $output but I don't know ho to achieve it elegantly
... if you want to have both immediate output and the output in a variable you can use popen() or proc_open(). you have full control over the pipes then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.