【问题标题】:PHP shell_exec(), exec(), and system() returning only partial outputPHP shell_exec()、exec() 和 system() 仅返回部分输出
【发布时间】:2013-08-18 00:13:52
【问题描述】:

我正在尝试使用 PHP 脚本来运行 siege 命令并捕获输出。

在 shell 中运行以下命令会提供这些结果:

$ /usr/local/bin/siege -c30 -t30s -f urls.txt

.....
HTTP/1.1 200   0.10 secs:   11246 bytes ==> GET  /*******.html
HTTP/1.1 200   0.11 secs:   11169 bytes ==> GET  /*******.html
HTTP/1.1 200   0.10 secs:   11246 bytes ==> GET  /*******.html

Lifting the server siege..      done.

Transactions:               1479 hits
Availability:             100.00 %
Elapsed time:              29.05 secs
Data transferred:          14.69 MB
Response time:              0.10 secs
Transaction rate:          50.91 trans/sec
Throughput:             0.51 MB/sec
Concurrency:                5.33
Successful transactions:        1479
Failed transactions:               0
Longest transaction:            0.16
Shortest transaction:           0.09

当通过 exec()、shell_exec()、system() 在 PHP 中运行相同的命令时,我只收到以下输出。

HTTP/1.1 200   0.10 secs:   11246 bytes ==> GET  /*******.html
HTTP/1.1 200   0.11 secs:   11169 bytes ==> GET  /*******.html
HTTP/1.1 200   0.10 secs:   11246 bytes ==> GET  /*******.html

由于我真的只对 siege 提供的结果感兴趣,所以这些数据对我来说毫无用处。由于某种原因,它忽略了围攻的结果。

这是我在 PHP 中所做的一个示例...

exec('/usr/local/bin/siege -c30 -t30s -f urls.txt', $output);

【问题讨论】:

    标签: php shell


    【解决方案1】:

    围攻程序将其输出写入两个不同的standard streamsstdoutstderr。 PHP 的exec() 只捕获stdout。要同时捕获两者,您需要将 redirect (using your shell) stderrstdout 以便所有内容都在 PHP 捕获的一个流中。

    为此,请在命令的最后添加2>&1。在您的示例中,这将是:

    exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);
    

    (我已经安装了 siege 并验证它同时使用 stdout 和 stderr 并且输出重定向有效。)

    【讨论】:

    • +1:干得好。显而易见的东西,但只有当您已经意识到应用程序可以输出到任一流时。手动使用终端并不是很清楚。
    • 完美运行。非常感谢!我能够使用以下内容来获取 stderr 输出 /usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1 >/dev/null 有没有更好的方法来做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多