How do I save or redirect stdout and stderr into different files?

by on May 23, 2007 · 0 comments· LAST UPDATED May 23, 2007

in , ,

Q. I need to run a program called oraMon.pl. However this program is run from cron job. It report error to stderr and normal output to stdout. How do I save stdout, stderr and both into 3 separate log files?

A. It is not that hard if you know howto redirect stderr, stdout and small command called tee.

=> fd0 is stdin
=> fd1 is stdout
=> fd2 is stderr

There are two formats for redirecting standard output and standard error:
&>word
and
>&word

For example anything written to fd2 to the same place as output to fd1, you will use:
2>&1

tee command read from standard input and write to standard output and file.

So to send stderr to /tmp/errors.log, stdout to /tmp/output.log and both to /tmp/final.log, type as follows:
((/path/to/oraMon.pl 2>&1 1>&3 | tee /tmp/errors.log) 3>&1 1>&2 | tee /tmp/output.log) > /tmp/final.log 2>&1

Read bash man page and tee command for more information.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: