php - PHP 未完全检索 Shell 输出!

标签 php linux shell command-line

我有一个执行 shell 命令的 PHP 脚本:

$handle = popen('python last', 'r');
$read = fread($handle, 4096);
print_r($read);
pclose($handle);

我回显 shell 输出的输出。当我在命令中运行它时,我得到如下信息:

[root@localhost tester]# python last
[last] ZVZX-W3vo9I: Downloading video webpage
[last] ZVZX-W3vo9I: Extracting video information
[last] ZVZX-W3vo9I: URL: x
[download] Destination: here.flv
[download]   0.0% of 10.09M at     ---b/s ETA --:--
[download]   0.0% of 10.09M at   22.24k/s ETA 07:44
[download]   0.0% of 10.09M at   66.52k/s ETA 02:35
[download]   0.1% of 10.09M at  154.49k/s ETA 01:06
[download]   0.1% of 10.09M at  162.45k/s ETA 01:03

但是,当我从 PHP 运行相同的命令时,我得到以下输出:

[last] ZVZX-W3vo9I: Downloading video webpage
[last] ZVZX-W3vo9I: Extracting video information
[last] ZVZX-W3vo9I: URL: x
[download] Destination: here.flv

如您所见,底部位丢失了,这是我需要的位!!之前的问题是百分比在同一行更新,但现在我已经更改了我的 Python 脚本,以便它创建一个新行。但这有所不同! :(

这个问题是相关的to this one .

感谢您的帮助。

更新

需要重定向输出“2>&1”。 Arul 很幸运 :P 因为我错过了选择属于 Pax 的正确答案的最后期限!

最佳答案

您只从管道中读取前 4,096 个字节,您需要将 fread/print_r 放在一个循环中并检查结束-使用 feof 函数的文件。

$handle = popen('python last', 'r');

while(!feof($handle))
{
    print_r(fread($handle, 4096));
} 

pclose($handle);

关于php - PHP 未完全检索 Shell 输出!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/529535/

相关文章:

java SSLHandshakeException 虽然证书受信任的系统范围

php - 如何在PHP中优化指数移动平均算法?

php - 您可以将 .php 文件添加到您的 .html 文件吗?

php - Propel 1.7 参数映射到 unix 时间

linux - stdin 进入 zip 命令,如何指定文件名?

linux - "export var=` echo ${var1}/.. .`"中的反引号和回显是否有原因?

php - 通讯队列的复杂 MySQL 查询

linux - 重定向本身就是参数的程序的输出

java - 列出多个jar文件的内容

python - 使用 Paramiko 在 Python 中通过 ssh 实现交互式 shell?