简体   繁体   English

PHP中的shell_exec()

[英]shell_exec() in PHP

<?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. 从浏览器执行上述脚本时,它将加载10秒钟,然后将脚本的输出转储到屏幕上。 This is, of 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? 但是,如果我希望将由Shell脚本写入STDOUT的数据实时显示在屏幕上,是否可以采取某些方法?

我将添加proc_open(),如果需要的话,它可以使您对命令执行有更多的控制,如果不尝试的话,可以尝试通过前面提到的passthru()或popen()。

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() 尝试passthru()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. 如下面@wik所建议的,您也可以尝试proc_open而不是popen,它应该以类似的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM