I am trying to use a PHP script to run the siege command and capture the output.
我正在尝试使用PHP脚本运行攻城命令并捕获输出。
Running the following in the shell provides these results:
在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
When running the same command in PHP via exec(), shell_exec(), system(), I only receive the following output.
当通过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
Since I'm really only interested in the results provided by siege, this data is useless to me. For some reason its ignoring the results of the siege.
因为我真的只对围攻提供的结果感兴趣,这个数据对我来说是没用的。出于某种原因,它无视围城的结果。
Here's an example of what I'm doing in PHP...
这是我在PHP中做的一个例子……
exec('/usr/local/bin/siege -c30 -t30s -f urls.txt', $output);
1 个解决方案
#1
4
The siege program writes its output to two different standard streams: stdout
and stderr
. PHP's exec()
only captures stdout
. To capture both, you need to redirect (using your shell) stderr
to stdout
so that everything is in the one stream that PHP captures.
攻城程序将输出写入两个不同的标准流:stdout和stderr。PHP的exec()只捕获stdout。要捕获这两个,需要将stderr重定向(使用shell)到stdout,以便PHP捕获的所有内容都位于一个流中。
To do this, add 2>&1
at the very end of your command. In your example, that would be:
为此,在命令末尾添加2个>和1。在你的例子中,那将是:
exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);
(I have installed siege and verified that it uses both stdout and stderr and that the output redirection works.)
(我已经安装了攻城,并验证了它同时使用stdout和stderr,并且输出重定向工作。)
#1
4
The siege program writes its output to two different standard streams: stdout
and stderr
. PHP's exec()
only captures stdout
. To capture both, you need to redirect (using your shell) stderr
to stdout
so that everything is in the one stream that PHP captures.
攻城程序将输出写入两个不同的标准流:stdout和stderr。PHP的exec()只捕获stdout。要捕获这两个,需要将stderr重定向(使用shell)到stdout,以便PHP捕获的所有内容都位于一个流中。
To do this, add 2>&1
at the very end of your command. In your example, that would be:
为此,在命令末尾添加2个>和1。在你的例子中,那将是:
exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);
(I have installed siege and verified that it uses both stdout and stderr and that the output redirection works.)
(我已经安装了攻城,并验证了它同时使用stdout和stderr,并且输出重定向工作。)