PHP shell_exec(),exec()和system()仅返回部分输出

the*_*art 6 php shell

我试图使用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
Run Code Online (Sandbox Code Playgroud)

通过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
Run Code Online (Sandbox Code Playgroud)

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

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

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

Tom*_*ers 4

siege 程序将其输出写入两个不同的标准流stdoutstderr。PHPexec()只捕获stdout. 要捕获两者,您需要重定向(使用 shell) stderr,以便stdout所有内容都位于 PHP 捕获的一个流中。

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

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

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