Automatically send stdout and stderror to a file as well as to the screen, but without using tee


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatically send stdout and stderror to a file as well as to the screen, but without using tee
# 1  
Old 04-24-2012
Automatically send stdout and stderror to a file as well as to the screen, but without using tee

Hi,

I've been using the following commands in my automated scripts, to ensure that all text output is sent to a log file instead of to the screen:
Code:
exec 1>>$SCRIPT_LOG_FILE
exec 2>>$SCRIPT_LOG_FILE

However, I've now discovered that the system used for automating the script executions actually saves all screen output to its own log file anyway. It would be useful for me to write all future scripts so that all stdout and stderr text is sent to the screen (to be saved to the automation log) as well as to the $SCRIPT_LOG_FILE specified above.

I'm aware that I could use something like this on each command within my scripts from now on:
Code:
command 2>&1 | tee -a $SCRIPT_LOG_FILE

...and this would give me the desired result, but it's a bit tedious to have to do that after every command within the script.

I'm also aware that I the scripts could be called with a tee argument:
Code:
my_script.sh 2>&1 | tee -a $SCRIPT_LOG_FILE

...and this would also give me the desired result, but I don't want to complicate the automated commands used to call scripts.

Finally, I'm also aware that I could use a 'wrapper script' for each one, and put a command like that above within it, and this would also have the desired effect, but it's a bit of a messy solution and I'd rather not have to double up on the number of script files I need, just to accomplish this.

Apologies if this has already been answered before, but I've searched quite a bit and haven't been able to find exactly what I'm looking for yet.

Thanks for any help provided. Smilie
# 2  
Old 04-24-2012
If you are using bash you can redirect to process.

Code:
exec > >(tee -a "$script_log_file") 2>&1

so everything from then on goes to tee

Last edited by neutronscott; 04-24-2012 at 11:47 AM.. Reason: edit: forgot a >
# 3  
Old 04-24-2012
Hi neutronscott,

Thanks for the quick reply. Unfortunately I'm using ksh on Solaris 10 (probably should have mentioned that), it's the standard shell used for all of our scripts. I saw that method and tried it already, but it didn't work under ksh. A pity as it's quite elegant!

I think I might have found something that does work though:

Code:
#!/bin/ksh
 
LOGFILE="logfile.log"
PIPEFILE="pipefile"
 
mkfifo $PIPEFILE
 
# Start tee writing to a logfile, but pulling its input from our named pipe.
tee $LOGFILE < $PIPEFILE &
 
# capture tee's process ID for the wait command.
TEEPID=$!
 
# redirect the rest of the stderr and stdout to our named pipe.
exec > $PIPEFILE 2>&1
echo "Make your commands here"
echo "All their standard out will get teed."
echo "So will their standard error" >&2
 
# close the stderr and stdout file descriptors.
exec 1>&- 2>&-
 
# Wait for tee to finish since now that other end of the pipe has closed.
wait $TEEPID

Taken from here: Force bash script to use tee without piping from the command line - Super User

This seems to work ok under ksh, but I'm still testing it...
# 4  
Old 04-24-2012
This is basically what that syntax does... Creates a piped background process. This code is good, except I always recommend to quote variable expansions (What if a filename ends up having a space in it?) and avoid all capital variables (What if we accidentally overlook its use as an environment/internal variable).

ie:
Code:
mkfifo "$pipefile"

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 04-24-2012
Thanks for the feedback Scott. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies

2. Shell Programming and Scripting

Redirect stdout and stderror in child process

I have a problem when i try to create a log file from a daemon process using shell scripting in ubuntu 12. Ultimatly what i want to achieve is run a java/jar file from a script. After scourging the internet i found several solutions to do this, the one i choose is to create a startup script that... (4 Replies)
Discussion started by: Narev
4 Replies

3. Shell Programming and Scripting

The pipe not use "tee" to print on the screen for specific function

I have code fragment like { aa bb cc } > $LOG aa bb cc, all call function "ff", I want "ff" to print on the screen,but others do not print on the scree, is there a method? I can't use "tee", becasue tee I meet the write "error" ff() { echo "hello" } (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Redirecting STDERR to file and screen, STDOUT only to file

I have to redirect STDERR messages both to screen and also capture the same in a file but STDOUT only to the same file. I have searched in this formum for a solution, but something like srcipt 3>&1 >&2 2>&3 3>&- | tee errs doesn't work for me... Has anyone an idea??? (18 Replies)
Discussion started by: thuranga
18 Replies

5. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

6. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

7. Shell Programming and Scripting

redirecting screen to file without tee

Hi All, I need to redirect screen o/p of shell script to file , but I don't want to use "tee" :Dcommand ./test.sh 2>&1 | tee /var/tmp/testing`date +'%d%h%Y%T'`.log but I want to write some code inside the shell script itself so wen I execute ./test.sh it will display o/p in screen... (3 Replies)
Discussion started by: tarunn.dubeyy
3 Replies

8. Shell Programming and Scripting

How Unix tee to send pipeline output to 2 pipes ?

Hi, I would like to process, filter the same ASCII asynchronous live data stream in more than one pipe pipeline. So the one pipeline should filter out some records using grep key word and more than one pipes pipelines each should grep for another key words, each set seperately for each... (5 Replies)
Discussion started by: jack2
5 Replies

9. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

10. Shell Programming and Scripting

perl : stdout is not return to screen

Hello All, I have a perl script , and the STDERR and additional FH is redirected to the STDOUT like below: open STDOUT ,">>$log" or die "$! :: $log\n"; open STDERR ,">&STDOUT" or die "$! :: Can redirect STDERR to STDOUT\n"; select STDERR; $|=1; open LOG ,">&STDOUT" or die "$! :: Can... (2 Replies)
Discussion started by: Alalush
2 Replies
Login or Register to Ask a Question