msgbartop
A chronological documentation test project, nothing serious, really!
msgbarbottom

04 Jun 2007 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 a 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.

Tags: , ,

Posted by

20 May 2007 Bash redirection

Theory and quick reference

There are 3 file descriptors, stdin, stdout and stderr (std=standard).

Basically you can:

  1. redirect stdout to a file
  2. redirect stderr to a file
  3. redirect stdout to a stderr
  4. redirect stderr to a stdout
  5. redirect stderr and stdout to a file
  6. redirect stderr and stdout to stdout
  7. redirect stderr and stdout to stderr

1 ‘represents’ stdout and 2 stderr.

A little note for seeing this things: with the less command you can view both stdout (which will remain on the buffer) and the stderr that will be printed on the screen, but erased as you try to ‘browse’ the buffer.

Sample: stdout 2 file

This will cause the ouput of a program to be written to a file.
ls -l > ls-l.txt

Here, a file called ‘ls-l.txt’ will be created and it will contain what you would see on the screen if you type the command ‘ls -l’ and execute it.

Sample: stderr 2 file

This will cause the stderr ouput of a program to be written to a file.

grep da * 2> grep-errors.txt

Here, a file called ‘grep-errors.txt’ will be created and it will contain what you would see the stderr portion of the output of the ‘grep da *’ command.

Sample: stdout 2 stderr

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

grep da * 1>&2

Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.

Sample: stderr 2 stdout

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

grep * 2>&1

Here, the stderr portion of the command is sent to stdout, if you pipe to less, you’ll see that lines that normally ‘dissapear’ (as they are written to stderr) are being kept now (because they’re on stdout).

Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

rm -f $(find / -name core) &> /dev/null

This (thinking on the cron entry) will delete every file called ‘core’ in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it’s output.

Source: http://en.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html#ss3.1

Tags: , ,

Posted by

09 Feb 2007 someprog > /dev/null 2>&1

The first part, “> /dev/null” means send standard output to the ‘bit bucket” or in other words, throw it away.

The second part, “2>&1” means “redirect standard error (2) to the same place as standard outout (1.)

Tags: , ,

Posted by