LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File output redirection - possible to redirect stream to multiple files? (https://www.linuxquestions.org/questions/linux-newbie-8/file-output-redirection-possible-to-redirect-stream-to-multiple-files-848857/)

EnderX 12-07-2010 08:53 AM

File output redirection - possible to redirect stream to multiple files?
 
I'm working on some scheduled task script files to keep nightly backups of some of our database information in place, and it's a bit annoying when they blow up. I know how to redirect stdout and stderr to a flat file I can view when I come in, and I know that 2>&1 maps them both to the same file (whatever was named in 1). However, I'm running into some cron-time situations where it's easier to have the two streams together, and other cron-time situations where it's easier to have them separated. I can't really tell which is going to happen; is there some way I could create both kinds of output file for my scripts, so that I've got a std_err only file and an interleaved std_out/std_err file?

Note: I've looked at the 'tee' command, but I don't think it will work for what I'm after. 'tee' appears to only work with stdout; I'm trying to work with stderr.

Any thoughts?

catkin 12-07-2010 09:31 AM

<your script> 2>&1 1>log.1 | tee log.stderr.1 > log.stderr.2

EnderX 12-07-2010 10:04 AM

I thank you for the code, and will try to figure out how to work with it, but it doesn't quite handle what I requested. I tested it against a dummied up script that I know will both produce successful output, and fail at the end. After the run, I ended up with a file log.1 which contained only the successful (stdout) output, and two identical files log.stderr.1 and log.stderr.2 which contained only the stderr output. Attempting to restore the stdout stream by removing the log.1 reference instead created two log.stderr files, again identical, which contained all of stderr followed by all of stdout, rather than interleaved. (Presumably this was the result of the piped call to tee, as an attempt to run without that but with the solo 2>&1 showed them in the originally expected order on the screen.)

What was the expected result of that code snippet? Would it have been log.stderr.1 with everything, and log.stderr.2 with only the stderr output, or vice versa?

ntubski 12-07-2010 12:09 PM

I don't think this is possible unless there is some way to distinguish stderr lines from stdout lines textually, eg they all start with "ERROR:". The problem is that copying output from a pipe using another process will lose the interleaving order and there is no way to get that back.

So the script either has to mark stderr lines somehow, or every line it sends to stderr it also sends to stdout.

crts 12-07-2010 02:07 PM

Quote:

Originally Posted by catkin (Post 4183574)
<your script> 2>&1 1>log.1 | tee log.stderr.1 > log.stderr.2

Hi,

the same result can be achieved with
Code:

script 1>log.1 2>log.2
@OP: This will create what you need:
Code:

{ /path/to/your_script 3>&1 1>&2 2>&3 | tee "/path/to/log.stderr" ;} &>"/path/to/log.all"

catkin 12-07-2010 10:39 PM

Quote:

Originally Posted by EnderX (Post 4183613)
What was the expected result of that code snippet?

The scrippet behaved as intended; I misunderstood your question :(


All times are GMT -5. The time now is 10:33 AM.