Redirect bash stdout+stderr to one file and stderr to another file

Luke Cousins

I need to redirect ALL output to one file, and in addition redirect stderr to another file. Can this be done easily?

Let's assume my command for the purposes of this example is:

php /tmp/doWork.php

I can get output to separate files by using:

php /tmp/doWork.php 1> /tmp/stdout_file 2> /tmp/stderr_file

Based on this, I attempted:

php /tmp/doWork.php &> /tmp/stdboth_file 2> /tmp/stderr_file

but this just put stdout and stderr in /tmp/stdboth_file and never wrote to /tmp/stderr_file.

Stéphane Chazelas

With zsh (and zsh only) and its multios feature:

your-cmd 2> stdout+stderr.log >&2 2> stderr.log

As fd 2 is redirected twice, zsh implements an internal tee to have it sent to both files.

With bash (or any Bourne-like shell (other than zsh where you'd need to disable multios for it to work here)), you can do the teeing by hand with:

{ your-cmd 2>&1 >&3 3>&- | tee stderr.log 3>&-; } > stderr+stdout.log 3>&1

(though you lose the exit status of your-cmd. zsh has it in $pipestatus[1], bash in "${PIPESTATUS[0]}" though (provided the redirection into stderr+stdout.log didn't fail)).

To record the pid of your-cmd, you could do:

{ sh -ec 'echo "$$" > /var/run/pidfile; exec your-cmd' 2>&1 >&3 3>&- |
   tee stderr.log 3>&-; } > stderr+stdout.log 3>&1

With yash and it's process redirection feature:

your-cmd > stdout+stderr.log 2>(tee stderr.log)

(but note that yash will not wait for the termination of that tee command, so the log files may not be complete by the time you run the next command after that).

Something similar (and with the same caveat) can be done with process substitution in bash, zsh and ksh93:

{ your-cmd 2> >(tee stderr.log); } > stderr+stdout.log

To run in background and get the pid:

(exec your-cmd 2> >(tee stderr.log)) > stderr+stdout.log & pid=$!

With rc:

{your-cmd |[2=0] tee stderr.log} > stdout+stderr.log

rc's pipes allow specifying which file descriptors are connected to the pipe. With other shells, it's always fd 1 of the left command and fd 0 of the right one (hence the little dance with fd 3 above to shift file descriptors around). rc will report failure if either your-cmd or tee fails, though the exact number may be lost.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

redirect stdout and stderr to one file, copy of just stderr to another

From Dev

bash: redirect stderr to file and stdout + stderr to screen

From Dev

How to redirect stdout to a file, and stdout+stderr to another one?

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Java

How to redirect and append both stdout and stderr to a file with Bash?

From Java

Redirect stderr and stdout in Bash

From Dev

How to redirect stdout and stderr to a file and display stderr to console?

From Java

Redirect Windows cmd stdout and stderr to a single file

From Dev

Unable to redirect stdout/stderr to log file

From Dev

Redirect stdout / stderr output to log file with timestamp

From Dev

POSIX compliant way to redirect stdout and stderr to a file

From Dev

Make a bash script to output STDOUT & STDERR to a file

From Dev

Append stderr and stdout to file

From Dev

Redirect script stderr & stdout to a file, but keep stdout to tty as well?

From Dev

How to redirect stderr to a file and some echos (not stdout) to the same file

From Dev

Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

From Dev

Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

From Dev

How to redirect STDERR to both file and console and STDOUT to file only

From Dev

How to redirect stderr to a file and some echos (not stdout) to the same file

From Dev

Trouble using dup2 to redirect stdout/stderr into the same file

From Dev

Redirect stdout/stderr of a background job from console to a log file?

From Dev

Laravel artisan queue:work redirect stderr/stdout to file

From Dev

Appending both stdout and stderr to file

From Dev

Bash - redirect stdout and stderr to files with background process

From Dev

echo stderr and stdout to file from bash script variable

From Dev

Logging stderr and stdout to log file and handling errors in bash script

From Dev

Bash Linux how to create stderr, stdout and a combined log file

From Dev

Redirect stdout to stderr in tcsh

Related Related

  1. 1

    redirect stdout and stderr to one file, copy of just stderr to another

  2. 2

    bash: redirect stderr to file and stdout + stderr to screen

  3. 3

    How to redirect stdout to a file, and stdout+stderr to another one?

  4. 4

    Redirect stdout and stderr to file and stderr to stdout

  5. 5

    Redirect stdout and stderr to file and stderr to stdout

  6. 6

    How to redirect and append both stdout and stderr to a file with Bash?

  7. 7

    Redirect stderr and stdout in Bash

  8. 8

    How to redirect stdout and stderr to a file and display stderr to console?

  9. 9

    Redirect Windows cmd stdout and stderr to a single file

  10. 10

    Unable to redirect stdout/stderr to log file

  11. 11

    Redirect stdout / stderr output to log file with timestamp

  12. 12

    POSIX compliant way to redirect stdout and stderr to a file

  13. 13

    Make a bash script to output STDOUT & STDERR to a file

  14. 14

    Append stderr and stdout to file

  15. 15

    Redirect script stderr & stdout to a file, but keep stdout to tty as well?

  16. 16

    How to redirect stderr to a file and some echos (not stdout) to the same file

  17. 17

    Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

  18. 18

    Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

  19. 19

    How to redirect STDERR to both file and console and STDOUT to file only

  20. 20

    How to redirect stderr to a file and some echos (not stdout) to the same file

  21. 21

    Trouble using dup2 to redirect stdout/stderr into the same file

  22. 22

    Redirect stdout/stderr of a background job from console to a log file?

  23. 23

    Laravel artisan queue:work redirect stderr/stdout to file

  24. 24

    Appending both stdout and stderr to file

  25. 25

    Bash - redirect stdout and stderr to files with background process

  26. 26

    echo stderr and stdout to file from bash script variable

  27. 27

    Logging stderr and stdout to log file and handling errors in bash script

  28. 28

    Bash Linux how to create stderr, stdout and a combined log file

  29. 29

    Redirect stdout to stderr in tcsh

HotTag

Archive