LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-07-2010, 08:53 AM   #1
EnderX
Member
 
Registered: Nov 2006
Posts: 63

Rep: Reputation: 15
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?
 
Old 12-07-2010, 09:31 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
<your script> 2>&1 1>log.1 | tee log.stderr.1 > log.stderr.2
 
Old 12-07-2010, 10:04 AM   #3
EnderX
Member
 
Registered: Nov 2006
Posts: 63

Original Poster
Rep: Reputation: 15
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?
 
Old 12-07-2010, 12:09 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

Rep: Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808
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.
 
Old 12-07-2010, 02:07 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by catkin View Post
<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"
 
Old 12-07-2010, 10:39 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by EnderX View Post
What was the expected result of that code snippet?
The scrippet behaved as intended; I misunderstood your question
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to redirect the output to multiple files without displaying it to the screen? ionrivera Programming 3 10-26-2010 01:04 AM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
Combine output of multiple files in one CSV file say_hi_ravi Programming 4 07-17-2008 03:04 AM
how to redirect stdout [binary stream] to multiple processes vtaminh Linux - General 2 08-19-2004 01:05 PM
Redirect output to multiple places granny Linux - Newbie 1 02-12-2004 12:31 PM


All times are GMT -5. The time now is 11:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration