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

标签 php shell

我正在尝试使用 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);

最佳答案

攻城程序将其输出写入两个不同的 standard streams : stdoutstderr。 PHP 的 exec() 只捕获 stdout。要捕获两者,您需要 redirect (using your shell) stderrstdout 以便所有内容都在 PHP 捕获的一个流中。

为此,请在命令的最后添加 2>&1。在您的示例中,这将是:

exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);

(我已经安装了 siege 并验证它同时使用 stdout 和 stderr 并且输出重定向有效。)

关于PHP shell_exec()、exec() 和 system() 仅返回部分输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18294532/

相关文章:

php - 如何在firefox操作系统中使用mysql数据库?

javascript - php中上传表单的进度条失败

javascript - 符号不能用ajax发布

php - 如何使用php计算多个数据库行中数字数组元素的总和?

linux - shell 脚本不工作

bash - 如果不存在,则设置环境变量的衬里,否则追加

bash - 用于检查字符串和特殊字符的 shell 脚本

php - 如何解决 Uncaught Error : Class 'mysqli' not found

git - 部分分支或部分 merge 或其他方式可以帮助我同步点文件?

正则表达式贪婪 : shrinking long path