2

I wanted to capture the output of the TCL script into the file using PHP. I am able to capture hello world output to the file but when I am running long script which takes time and have large output then I am not.

Here is my code:

 <?php
 ob_start();
 passthru(' /path/to/file/helloworld ');
 $out1 = ob_get_contents();
 ob_end_clean();
 $fp = fopen('/path/to/file/output.txt',w);
 fwrite($fp,$out1);
 fclose($fp);
 echo'<pre>', $out1,'</pre>';
 #var_dump($out1);
 ?>

Please suggest me what is wrong with the long TCl scrip.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
pgupta
  • 21
  • 3
  • 2
    Check out http://stackoverflow.com/questions/3407939/shell-exec-timeout-management-exec. Your script might be timing out. – Andrew Cheong Jun 14 '13 at 20:48
  • You've shown no evidence that the problem lies in the helloworld script. A google search for "php passthru timeout" reveals some relevant SO questions. – glenn jackman Jun 14 '13 at 20:58

1 Answers1

1

EDIT: For long running scripts (like daemons) i recommend using popen and stream the content out of the resource.

Example:

<?php
$h = popen('./test.sh', 'r');                                                                                                                                                        
while (($read = fread($h, 2096)) ) {                                                                                                                                                 
    echo $read;
    sleep(1);                                                                                                                                                                        
}
pclose($h);

You should check your php.ini for the "max_execution_time". If you are in a webserver context also check for configured timeouts there.

END OF EDIT

Have you tried exec

The second parameter is a reference to an array which gets filled with the scripts output

in short:

<?php
$output = array();
exec('/path/to/file/helloworld', $output);
file_put_contents('/path/to/file/output.txt', implode("\n", $output));

Example:

test.sh:

#!/bin/bash                                                                                                                                                                          
echo -e "foo\nbar\nbaz";                                                                                                                                                             
echo -e "1\n2\n3"; 

test.php:

<?php

$output = array();
exec('./test.sh', $output);
var_dump($output); 

output:

php test.php
array(6) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(3) "baz"
  [3]=>
  string(1) "1"
  [4]=>
  string(1) "2"
  [5]=>
  string(1) "3"
}

Quote of the official php documentation (link see above)

If the output argument is present, then the specified array will be filled with every line of output from the command.

regexp
  • 766
  • 4
  • 14
  • `exec` only returns the last line of output. `passthru` is correct in this case. (I've undone my downvote since you're new.) – Andrew Cheong Jun 14 '13 at 20:47
  • @acheong87 it returns only last line, but here no return is stored. `$output` is passed as reference and filled with every line. – dev-null-dweller Jun 14 '13 at 20:52
  • @dev-null-dweller - Ah, sorry, yeah, I didn't see that. I'm pretty sure this user posted the same question yesterday, and he was using `exec`, so I jumped to the conclusion that someone was suggesting back to `exec`. I think his problem really is a timeout though. – Andrew Cheong Jun 14 '13 at 20:55
  • So anyone is having any idea that how to proceed with timeout for the long script. any example would be the great help !! – pgupta Jun 14 '13 at 21:27