![](/img/trans.png)
[英]php runs terminal app with system()/shell_exec()/exec() but only half of the output shows up
[英]PHP shell_exec(), exec(), and system() returning only partial output
我試圖使用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
因為我真的只對圍攻提供的結果感興趣,所以這些數據對我來說毫無用處。 由於某種原因,它無視圍攻的結果。
這是我在PHP中所做的一個例子......
exec('/usr/local/bin/siege -c30 -t30s -f urls.txt', $output);
圍攻程序將其輸出寫入兩個不同的標准流 : stdout
和stderr
。 PHP的exec()
只捕獲stdout
。 要捕獲這兩者,您需要將 stderr
(使用shell)重定向到stdout
以便所有內容都在PHP捕獲的一個流中。
為此,請在命令的最后添加2>&1
。 在您的示例中,那將是:
exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);
(我已經安裝了siege並驗證它使用了stdout和stderr以及輸出重定向是否正常工作。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.