我有一些奇怪的行为,从 s3cmd 重定向 stdout 和 stderr。例如:
$ touch test
$ s3cmd put test s3://non_existent_bucket
test -> s3://non_existent_bucket/test [1 of 1]
4 of 4 100% in 0s 88.17 B/s done
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist
这个输出(如果我没有理解错的话)部分是标准输出,部分是标准错误。如下所示:
$ s3cmd put test s3://non_existent_bucket 2> log
test -> s3://non_existent_bucket/test [1 of 1]
4 of 4 100% in 0s 120.11 B/s done
$ cat log
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist
实际上,stderr 被重定向,但我们需要 stdout 和 stderr。让我们先尝试仅标准输出:
$ s3cmd put test s3://non_existent_bucket > log
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist
$ cat log
它不是写在命令之后,所以我理解 stdout 实际上是重定向(或其他东西),但它不会被记录在日志文件中。我猜它会以某种方式检测它是否连接到 tty,并且仅在第一种情况下输出此类数据。
以同样的方式,如果我尝试重定向两个流,我会得到 stderr,但不会得到我需要的 stdout 输出:
$ s3cmd put test s3://non_existent_bucket >& log
$ cat log
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist
事实上,我需要做的是从 PHP 调用它并获取完整的输出。我只找到了执行命令和检索标准输出的方法,因此我正在执行以下操作:
exec('s3cmd put test s3://non_existent_bucket 2>&1', $output);
在这里,我没有从两个流中获得任何输出。我想我失去了 stdout,因为 s3cmd 检测到它没有连接到 tty,但 stderr 我仍然不明白为什么它没有被捕获。
尝试使用 popen() 和 exec() 我得到相同的结果。
我需要整个输出,或者至少是标准输出(尽管我猜进度信息会以某种方式被抓取)。有什么想法吗?
提前致谢!!
最佳答案
man page for s3cmd详细说明--progress
/--no-progress
标志,描述如下:
Display or don't display progress meter. When running on TTY (e.g. console or xterm) the default is to display progress meter. If not on TTY (e.g. output is redirected somewhere or running from cron) the default is to not display progress meter.
所以看起来 s3cmd 会执行类似 isatty(0)
的操作来确定是否将进度详细信息写入 STDOUT,但如果您显式指定 --[no-]进度
它将强制一种行为或另一种行为。
尝试:
exec('s3cmd --progress put test s3://non_existent_bucket 2>&1', $output);
关于php - 使用 s3cmd 进行棘手的 stderr 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311444/