dongtie0929 2012-07-03 12:58
浏览 107
已采纳

使用s3cmd进行棘手的stderr重定向

I have some strange behavior redirecting stdout and stderr from s3cmd. For example:

$ 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

This output is (if I'm not getting it wrong) part stdout and part stderr. That is shown as follows:

$ 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

Effectively, stderr is redirected, but we want both stdout and stderr. Let's try only stdout first:

$ s3cmd put test s3://non_existent_bucket > log
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist

$ cat log

It is not written after the command so I understand stdout is in fact redirecting (or something), but it doesn't get caught on the log file. I guess it somehow detects if it is connected to a tty or not, and only outputs this kind of data in the first case.

In the same way, if I try to redirect both streams, I get stderr but not the stdout output that I need:

$ s3cmd put test s3://non_existent_bucket >& log

$ cat log
ERROR: S3 error: 404 (NoSuchBucket): The specified bucket does not exist

In fact, what I need to do is call this from PHP and get the full output. I've only found ways to execute a command and retrieve stdout, so I'm doing as follows:

exec('s3cmd put test s3://non_existent_bucket 2>&1', $output);

And here, I don't get any of the output from either streams. I guess I'm loosing stdout because s3cmd detects it's not connected to a tty, but stderr I still don't understand why it's not caught.

Trying with popen() and exec() I get the same result.

I need the whole output, or stdout at least (although I guess the progress info will be somehow grabbled). Any idea?

Thanks in advance!!

  • 写回答

1条回答 默认 最新

  • douchao1879 2012-07-03 13:20
    关注

    The man page for s3cmd details a --progress/--no-progress flag, with a description as follows:

    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.

    So it looks like s3cmd will do something like isatty(0) to determine whether to write the progress details to STDOUT or not, but if you explicitly specify --[no-]progress it will force one behaviour or the other.

    Try:

    exec('s3cmd --progress put test s3://non_existent_bucket 2>&1', $output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?