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
2009-03-12 09:16:20
I was searching SO for how to do this with pipe and tee. You da man!
Ogre Psalm33
2010-08-04 12:54:09
+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
2009-03-12 09:17:03
+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
2009-03-12 09:17:20
Lars Wirzenius
2009-03-12 09:25:53
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
2009-03-12 09:34:55
I tend to forget that... as you can see. I made the fix and added the post to community wiki
f3lix
2009-03-12 09:49:04
ubermonkey
2009-05-27 14:04:03
SlappyTheFish
2010-06-08 10:58:19
SlappyTheFish
2010-06-08 11:17:41
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
2009-09-01 14:14:31
That's strange, I'm trying to roll it back, and it keeps putting the new text in instead.
R. Bemrose
2009-09-01 14:29:40
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
2009-09-01 14:31:41
+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 ;(