views:

9864

answers:

6

I want to redirect both stdout and stderr of a process to a single file. How do I do that in bash?

+5  A: 
do_something 2>&1 | tee -a some_file

This is going to redirect everything to file and print it to stdout.

Dev er dev
I was searching SO for how to do this with pipe and tee. You da man!
Ogre Psalm33
+3  A: 
bash your_script.sh 1>file.log 2>&1

1>file.log instructs the shell to send STDOUT to the file file.log, and 2>&1 tells it to redirect STDERR (file descriptor 2) to STDOUT (file descriptor 1).

Note: The order matters as liw.fi pointed out, 2>&1 1>file.log doesn't work.

Guðmundur H
+10  A: 

You can redirect stderr to stdout and the stdout into a file:

some_command 1>file.log 2>&1

See http://tldp.org/LDP/abs/html/io-redirection.html

EDIT: changed the order as pointed out in the comments

f3lix
Lars Wirzenius
Good point, I seem to have been doing this wrong all these years... no wonder I get all those emails from cron. Thanks!
Guðmundur H
I tend to forget that... as you can see. I made the fix and added the post to community wiki
f3lix
ubermonkey
SlappyTheFish
SlappyTheFish
+8  A: 

Take a look here. Should be:

yourcommand &>filename

(redirects both stdout and stderr to filename).

dirkgently
Somebody should restore to the second edit of this comment. Supplementary info to the question shouldn't be removed, especially in a 6 month old answer.
Autocracy
That's strange, I'm trying to roll it back, and it keeps putting the new text in instead.
R. Bemrose
There's apparently an issue with rolling things back at the moment. I'm getting the fail cat page (sorry, term swiped from Twitter's fail whale page).
R. Bemrose
still not rolled back
corydoras
+1  A: 
LOG_FACILITY="local7.notice"
LOG_TOPIC="my-prog-name"
LOG_TOPIC_OUT="$LOG_TOPIC-out[$$]"
LOG_TOPIC_ERR="$LOG_TOPIC-err[$$]"

exec 3>&1 > >(tee -a /dev/fd/3 | logger -p "$LOG_FACILITY" -t "$LOG_TOPIC_OUT" )
exec 2> >(logger -p "$LOG_FACILITY" -t "$LOG_TOPIC_ERR" )

It is related: Writing stdOut & stderr to syslog.

It almost work, but not from xinted ;(

+2  A: 

Curiously, this works:

yourcommand &> filename

But this gives a syntax error:

yourcommand &>> filename
syntax error near unexpected token `>'

You have to use:

yourcommand 1>> filename 2>&1