2013-08-18 53 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

相关问题