【问题标题】:How to pipe output to stdout and into another process in docker如何将输出通过管道传输到标准输出并进入 docker 中的另一个进程
【发布时间】:2018-12-03 04:27:06
【问题描述】:

我想通过管道传输在 docker 容器内运行的程序的标准输出,同时将输出保存在 docker 日志(即标准输出)中。

另一个 stackoverflow 问题 (How to pipe stdout while keeping it on screen ? (and not to a output file)) 建议使用 foo | tee /dev/tty | bar,但是当 docker 在非交互模式下运行时这不起作用,因为 /dev/tty 不存在。

这可能吗?

【问题讨论】:

  • foo | tee >(bar) ?
  • 这似乎可以工作,只要我使用 bash 而不是 sh 这很好。随意将此评论更改为答案,以便我可以勾选它

标签: linux shell docker


【解决方案1】:

您想复制一个名为foo 的程序的标准输出,以将其输出到您的脚本标准输出,并将其通过管道传送到另一个名为bar 的程序。这正是tee 的用途。

foo | tee >(bar)

如果您没有支持进程替换的 shell,您可以在多行中执行相同操作,方法是创建一个先进先出并在后台运行 cat $fifo 并运行 too "$fifo" 将流复制到标准输出和先进先出:

fifo=$(mktemp -u); mkfifo $fifo;
cat "$fifo" &
foo | tee "$fifo" | bar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多