dongqu4443 2013-08-18 00:13
浏览 167
已采纳

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

I am trying to use a PHP script to run the siege command and capture the output.

Running the following in the shell provides these results:

$ /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.

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...

exec('/usr/local/bin/siege -c30 -t30s -f urls.txt', $output);
  • 写回答

1条回答 默认 最新

  • doumingo04696 2013-08-18 01:03
    关注

    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.

    To do this, add 2>&1 at the very end of your command. In your example, that would be:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100