2013-08-18 52 views
6

我想用PHP腳本來運行siege命令並捕獲輸出。PHP shell_exec(),exec()和system()只返回部分輸出

運行在殼體下面提供了這些結果:

$ /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在PHP運行相同的命令(),了shell_exec(),系統(),我只收到以下輸出。

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); 

回答

4

圍困程序的輸出寫入到兩個不同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); 

(我已經安裝了圍攻並驗證它使用標準輸出和標準錯誤和輸出重定向工作。)

+0

+1:幹得好。明顯的東西,但只有當你已經知道應用程序可以輸出到任何一個流。手動操作終端並不是很清楚。 –

+0

完美地工作。非常感謝!我可以使用以下命令獲得stderr輸出 '/ usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1>/dev/null' 有沒有更好的方法來做這個? – theseanstewart

相關問題