LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2009, 09:22 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
bash logging stdout plus stderr


Hi.

I want to catch some outputs in two different log files in bash, file simple.log and all.log

So far, the script is started like this:

Code:
 xterm -e "(./myscript.sh | tee -a simple.log) >& all.log"
What I want is:
- In simple.log, I want all the stdout but WITHOUT errors.
- In all.log I want BOTH stdout and stderr.

So all is ok, and the output becomes:

all.log
Code:
 starting copying files
 mv: cannot move ... : permission denied
 copying completed
simple log
Code:
 starting copying files
 copying completed

But, in the new xterm, I'm loosing stdout. There is no output.How can I have the files logged as they are but also have the stdout in the xterm.

I've been playing around with piping but I can't get around the problem.

Any Ideas?

Thanks,
Indy
 
Old 01-26-2009, 12:06 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
How about
Code:
xterm -e sh -c 'TTY=`tty` ; (./myscript.sh | tee -a simple.log | tee $TTY) &> all.log'
Or a bit simpler if you don't mind standard error on the xterm as well:
Code:
xterm -e sh -c '(./script.sh | tee -a simple.log) 2>&1 | tee all.log'
 
Old 01-27-2009, 01:50 AM   #3
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Hi ntubski,

Thanks for your answer. The first option didn't work for some reason. I haven't failed trace it where it breaks though. The xterm window just starts and gets kill. And nothing written on any logs. The main reason I could think of is that it's a bash script and not a sh script. I even tried "-c bash 'TTY=`tty`" ; ... " but with no change.

I can't use the second option because I don't want the stderr on the xterm.


Indy
 
Old 01-27-2009, 07:21 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Ok,

The only solution I could make work was:
Code:
xterm -e "(./myscript.sh | tee -a simple.log) >& all.log | tail -f simple.log"
if anyone is interested.
 
Old 01-27-2009, 08:26 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, if you're not interested in seeing the stderr in the terminal (since you tail -f the simple.log containing the stdout only) you can simply do something like
Code:
 xterm -e "(./my_script.sh 2>> all.log | tee -a simple.log all.log)"
this will redirect the stderr to all.log appending it, while the tee command accepts multiple arguments to redirect the standard output to multiple files at once.
 
Old 01-27-2009, 01:49 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
The first option didn't work for some reason.
I tested before with rxvt, which I thought acts the same way, but apparently not. I got this to work:
Code:
xterm -e 'TTY=`tty` ; (./myscript.sh | tee -a simple.log | tee $TTY) &> all.log'
What confuses me is that according to the man page (of both rxvt and xterm) the -e option takes a program name and arguments not a string to be passed to a shell, so it seems like this shouldn't work.
Code:
       -e program [ arguments ... ]
               This  option  specifies  the  program (and its command line arguments) to be run in the
               xterm window.  It also sets the window title and icon name to be the  basename  of  the
               program  being  executed if neither -T nor -n are given on the command line.  This must
               be the last option on the command line.
This also works, maybe xterm has an undocumented feature of passing the -e's argument to bash if it fails to execute as a program.
Code:
xterm -e bash -c 'TTY=`tty` ; (./myscript.sh | tee -a simple.log | tee $TTY) &> all.log'
 
Old 01-29-2009, 09:16 AM   #7
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Thanks colucix

Exactly what I was looking for.
And now when looking at the syntax, it clearly makes sense.


Indy
 
  


Reply



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
stderr vs stdout sniffer_raghav Programming 2 09-05-2008 10:00 AM
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 01:57 AM
redirecting BASH script stdout/stderr from the script itself Hewson Linux - General 4 04-18-2008 03:32 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Reopen the stdout and stderr rahul_kulkarni Programming 3 02-21-2005 06:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration