Monday, February 25, 2008

linux basics: IO redirection - append to both stderr and stdout

It is possible to redirect one output channel to another like "2>&1" which means "put the output of channel 2 (stderr) where channel 1 (stdout) currently goes" and let channel 1 point to a file:

process >>file 2>&1



But it is preferable that you first try with separately mentioned files - it makes a much easier to maintain code AND it is not position dependent:

process >file 2>&1
will have stderr and stdout go to , but:


process 2>&1 >file
will have stdout go to and stderr to the screen, because when channel 2 intended direction is evaluated, channel 1 is still pointing to the terminal and not to the file.


This is one of the most common errors in crontabs and responsible for an awful lot of unnecessary (and unwanted) mails to root (the cronjobs replacement for a terminal).

No comments: