PHP 中的 shell_exec()

发布于 2024-08-27 18:49:48 字数 294 浏览 8 评论 0 原文

<?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
?>

当上面的脚本从浏览器执行时,它会加载 10 秒并将脚本的输出转储到屏幕上。这当然是正常的。但是如果我希望shell脚本写入STDOUT的数据实时显示在屏幕上,有什么办法可以做到吗?

<?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, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛泛之交 2024-09-03 18:49:49

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

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.

彩虹直至黑白 2024-09-03 18:49:49

试试这个:

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

这对我来说非常有用。

Try this:

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

It works great for me.

千仐 2024-09-03 18:49:49

尝试 passthru()popen()

代码看起来像这样:

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

正如下面@wik建议的那样,你也可以尝试 proc_open 而不是 popen 它应该可以工作以类似的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文