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

Do you really want to capture ALL remaining session? Above exec s aren't necessarily the killall tool for what you need. Howsoever, did you consider tee ing output to /dev/tty ?

You will need to restore file descriptors to their original values.
This can be done from inside the script in point of code you like.

During runtime, outside of the script, possibly with some gdb hackery, but i would advise against it.

Example for both operations can be found by searching keywords above.

Hope that helps.
Regards
Peasant.

One file descriptor can't write to two streams by itself. You'll need tee or something else to accomplish this.

I usually use this paradigm at the top of a script:

# Redirect the stdout/stderr to screen AND log file

mkfifo /tmp/$$-err /tmp/$$-out
# to merge stdout/stderr to log file AND screen
( exec tee -a ${FILE_LOG} </tmp/$$-out ) &
( exec tee -a ${FILE_LOG} </tmp/$$-err >&2 ) &

exec 1>/tmp/$$-out
exec 2>/tmp/$$-err
rm -f /tmp/$$-out /tmp/$$-err
1 Like

That is a really clever way to do it. I will shamelessly steal that immediately and include it into my own scripts. Thank you!

bakunin

1 Like

Two file handles for the same file? I guess this can loose some previous writes when switching to the other handle.
Correct is

exec 1>>/tmp/tmp.log 2>&1