You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用shell的tee命令无法捕获sam build全部输出的问题

问题:sam build构建过程日志无法通过tee写入文件

执行sam build时,终端会正常输出完整的构建步骤(比如各个Lambda函数的构建阶段),但使用管道命令sam build | tee build.txt将输出同步保存到文件时,build.txt中仅包含最终的构建成功总结,中间的所有步骤日志都未被写入。

终端典型输出

sh-4.2# sam build | tee build.txt
Building codeuri: /var/task/lambda-functions/scaffold-site-codebase runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ScaffoldSiteCodebaseFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
# 省略其他函数构建步骤...

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

生成的build.txt内容

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
解决方法

问题根源是SAM CLI将构建过程的步骤日志输出到了标准错误流(stderr),而tee命令默认仅捕获标准输出流(stdout),因此中间步骤没有被写入文件。只需将stderr重定向到stdout,就能让tee捕获全部输出:

sam build 2>&1 | tee build.txt

命令解释

  • 2>&1:将文件描述符2(stderr)的输出重定向到文件描述符1(stdout),让所有输出合并到同一个流中
  • 这样tee就能同时捕获构建步骤日志和最终总结,既在终端显示,又写入build.txt文件

内容的提问来源于stack exchange,提问作者fudo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 15:53:33