开发者

shell_exec() in PHP

开发者 https://www.devze.com 2022-12-26 17:44 出处:网络
<?php // Execute a shell script $dump = shell_exec(\'bigfile.sh\'); // This script takes some 10s to complete execution
<?php
  // Execute a shell script
  $dump = shell_exec('bigfile.sh'); // This script takes some 10s to complete execution
  print_r($dump); // Dump log to screen
?>

When the script above is executed from the browser, it loads for 10s and the dumps the output of the script to the screen. This is, o开发者_开发问答f course, normal. But if I want the data written to STDOUT by the shell script to be displayed on the screen in real-time, is there some way I could do it?


I would add proc_open() which gives you much more control over command execution if you need it, if not try passthru() or popen() as it was mentioned before.


Try this:

$handle = proc_open('bigfile.sh', array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes);
$status = proc_close($handle);

It works great for me.


Try passthru() or popen()

The code will look something like this:

<?php 
        $fp=popen("bigfile.sh","r");
        while (!feof($fp)) { 
                $results = fgets($fp, 256); 
                echo $result; 
                flush(); 
        } 
?> 

As @wik suggest below you can also try proc_open instead of popen it should work in a similar fashion.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号