shellpipetee

shell tee not redirecting all command output to file


I'm running a command that output some lines of code during execution and some final ones when finished, but piping it to tee to have that output duplicated to file write just the final lines.

The command is sam build, and typical ouput is as follows, but only the last section (the result recap) is written to file.

Typical command output

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
Building codeuri: /var/task/lambda-functions/confirm-new-purchase runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ConfirmNewPurchaseFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/register-new-customer runtime: nodejs18.x metadata: {} architecture: x86_64 functions: RegisterNewCustomerFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/send-new-purchase-emails runtime: nodejs18.x metadata: {} architecture: x86_64 functions: SendNewPurchaseEmailsFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/send-failed-subscription-renewal-email runtime: nodejs18.x metadata: {} architecture: x86_64 functions: SendFailedSubscriptionRenewalEmailFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/create-stripe-customer runtime: nodejs18.x metadata: {} architecture: x86_64 functions: CreateStripeCustomerFunction
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

file content


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

Solution

  • You can pass both stderr and stdout from sam build to tee like this:

    sam build 2>&1 | tee build.txt