≡ Menu

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

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.

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 0 comments… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: