2

There are plenty of threads here discussing how to do this for scripts or for the cmdline (mostly involving pipes, redirections, tee). What I didn't find is a solution which can be set up once and then just works globally, without manipulating single scripts or adding something to every command line.

What I want to achieve is something like described in the top answer of How do I write stderr to a file while using "tee" with a pipe?

Isn't it possible to configure the bash session so that all stderr output is logged to a file, while still writing it to console? Something I could add to .bashrc and thus automatically set up every time I login?

Software: Bash 4.2.24(1)-release (x86_64-pc-linux-gnu), xterm, Ubuntu 12.04

Community
  • 1
  • 1
didi_X8
  • 5,018
  • 10
  • 42
  • 46
  • you are right, apologies. The method I had described only works for joining the pipes, not for the purpose you want. I'm at a loss. Deleted my answer. – 0xC0000022L Jul 06 '12 at 17:30
  • I don't know how to do only `stderr`, but I do know how to do all output displayed. Would that work for you? – cha0site Jul 06 '12 at 18:05

1 Answers1

0

Try this variation on @0xC0000022L's previous solution (put it in your .bash_profile):

exec 2> >( tee log.file > /dev/tty )

A couple of caveats:

  1. The prompt and anything you type at the command line are printed to stderr, and so will be logged in your file.

  2. There could be an issue with the newline that terminates a command not being displayed in your terminal; I observe it on my Linux host, but not on my Mac OS X laptop. Perhaps someone else can explain and/or fix the issue. For example, if I type "echo stdout", I see the following:

    $ echo stdoutstdout
    $
    
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Append to `/dev/tty` to avoid losing the newline (I don't know why). `exec 2> >( tee -a log.file >> /dev/tty )` You'll also want to append to the log file using `-a`. – Dennis Williamson Jul 06 '12 at 18:38
  • heureka! A note: the file may be written with some latency. In my first quick test it was flushed only after closing the session. Will report back if I discover any issues. Thanks! – didi_X8 Jul 07 '12 at 20:24