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

TWiStErRob

I know how to redirect to a file, and use tee; on a basic level. So

$ alias outanderr='bash -c "echo stdout >&1; echo stderr >&2"'
# A fake "application" displaying both output and error messages.

$ outanderr 1>file      # redirect stdout to a file, display stderr
stderr

$ outanderr 2>file      # redirect stderr to a file, display stdout
stdout

$ outanderr 1>file 2>&1 # redirect both to a file, display nothing

$ outanderr | tee file; echo "-- file contents --" && cat file
# redirect stdout to a file, display both (note: order is messed up)
stderr
stdout
-- file contents --
stdout

$ outanderr 2>&1 | tee file; echo "-- file contents --" && cat file
# redirect both to a file, display both
stdout
stderr
-- file contents --
stdout
stderr

The question is: what to write in place of the questionmarks to get the output below:

$ outanderr ???; echo "-- file contents --" && cat file
# redirect both to a file, display stderr
stderr
-- file contents --
stdout
stderr

Constaints:

  • Assuming bash.
  • The order should be kept in the file.
  • stderr contents are displayed in real time line by line, i.e. no buffering.
  • Separate script files can be used.
  • Magic may be necessary.
Hauke Laging
2>&1 >>outputfile | tee --append outputfile

For easy testing:

echo -n >outputfile; bash -c "echo stdout >&1; echo stderr >&2" 2>&1 >>outputfile |
  tee --append outputfile; echo "outputfile:"; cat outputfile

Edit 1:

This works by writing stdout (only) to the file, making sterr stdout so that it goes through the pipe, and having tee write its output to the same file.

Both writes must be done in append mode (>> instead of >) otherwise both would overwrite each others output.

As the pipe is a buffer there is no guarantee that the output appears in the file in the right order. This would not even change if an application was connected to both file descriptors (two pipes). For guaranteed order both outputs would have to go through the same channel and be marked respectively. Or you would need some really fancy stuff:

  1. If both stdout and stderr were redirected to a file (not the same file!) and both files were on a FUSE volume then the FUSE module could mark each single write with a timestamp so that a second application could sort the data correctly and combine it for the real output file. Or you don't mark the data but have the module create the combined output file. Most probably there is no FUSE module yet which does this...
  2. Both stdout and stderr could be directed to /dev/null. The outputs of the application would be separated by running it through strace -f -s 32000 -e trace=write. You would have to reverse the escaping in that case. Needless to say that the application does not run faster by being traced.
  3. Maybe the same could be reached by using an existing, simple FUSE module and tracing the module instead of the application. This may be faster than tracing the application because (or rather: if) the module probably has a lot less syscalls than the application.
  4. If the application itself can be modified: The app could be stopped after each output (but I think this is possible from the inside only) and continue only after receiving s signal (SIGUSR1 or SIGCONT). The application reading from the pipe would have to check both the pipe and the file for new data and to send the signal after each new data. Depending on the kind of application this may be faster or even slower than the strace method. FUSE would be the maximum speed solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to redirect stderr in a variable but keep stdout in the console

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 Dev

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

From Dev

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

From Dev

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

From Dev

How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

From Dev

How to redirect stderr and stdout to different files and also display in terminal?

From Dev

How to redirect stderr and stdout to different files and also display in terminal?

From Dev

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

From Dev

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

From Dev

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

From Java

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

From Dev

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

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 Java

Redirect stderr and stdout in Bash

From Dev

Redirect stdout to stderr in tcsh

From Dev

Permanently redirect stderr to stdout

From Dev

How to redirect the raw_input to stderr and not stdout?

From Dev

How to redirect error output to both stdout and to stderr?

From Dev

Linux : how to redirect stdout & stderr to logger?

From Dev

How to redirect a part of stderr and stdout to /dev/null

From Dev

How can I redirect stdout and stderr with variant?

From Dev

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

From Dev

Append stderr and stdout to file

Related Related

  1. 1

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

  2. 2

    How to redirect stderr in a variable but keep stdout in the console

  3. 3

    Redirect stdout and stderr to file and stderr to stdout

  4. 4

    Redirect stdout and stderr to file and stderr to stdout

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

    How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

  9. 9

    How to redirect stderr and stdout to different files and also display in terminal?

  10. 10

    How to redirect stderr and stdout to different files and also display in terminal?

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

    Redirect Windows cmd stdout and stderr to a single file

  17. 17

    Unable to redirect stdout/stderr to log file

  18. 18

    Redirect stdout / stderr output to log file with timestamp

  19. 19

    POSIX compliant way to redirect stdout and stderr to a file

  20. 20

    Redirect stderr and stdout in Bash

  21. 21

    Redirect stdout to stderr in tcsh

  22. 22

    Permanently redirect stderr to stdout

  23. 23

    How to redirect the raw_input to stderr and not stdout?

  24. 24

    How to redirect error output to both stdout and to stderr?

  25. 25

    Linux : how to redirect stdout & stderr to logger?

  26. 26

    How to redirect a part of stderr and stdout to /dev/null

  27. 27

    How can I redirect stdout and stderr with variant?

  28. 28

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

  29. 29

    Append stderr and stdout to file

HotTag

Archive