Berkeley CSUA MOTD:Entry 35210
Berkeley CSUA MOTD
2017/10/18 [General] UID:1000 Activity:popular

2004/12/8 [Computer/SW/Virus, Computer/SW/Unix] UID:35210 Activity:moderate
12/7      I'd like to run a program and save the output to a log file
           while still seeing the program output on stdout.  I tried using
           the tee command as in "foo.exe | tee mylog.txt" but tee only
           seems to print to stdout every once in a while instead of when
           foo.exe generates a line of output.  How do I save output to a file
           while having every new line of output sent to stdout?  Thanks. -emin
           \_ The problem is not in tee, but in foo.  By default, the stdio
              library produces output a line at a time if it's outputting
              directly to a terminal, but buffers its output in large chunks
              otherwise (see "man setvbuf").  When you pipe foo's output to
              another program, it's no longer outputting to a terminal, so it
              turns on its buffering.  The easiest cure is to create a fake
              terminal for it to run on: ssh -t localhost foo.exe | tee mylog.txt
              I know, it sucks.  The default buffering really ought to be
              smarter, or at least configurable.  --mconst
            \_ foo and tee BOTH buffer, don't they?
                 \_ Tee actually never buffers its output.  Even if it used the
                    default stdio buffering, though, it wouldn't be a problem
                    here since it's outputting directly to a terminal.  --mconst
                  \_ what about foo | cat | tee mylog.txt?
                     \_ That won't help anything.  foo is still writing to a
                        \_ The mconst has spoken.  Woe to those who will not
         \_ You have to redirect stderr to stdout. In bourne-like shells,
            foo.exe 2>&1 | tee log
            In csh derivatives, I think it's something like
            foo.exe |& tee log
         \_ Another possibility you might explore is using 'screen' to run your
            process, with screen logging to a log file. SCREEN RULES!!
         \_ "Sounds like a virus.  Reformat and start over."
             \_ Advice like this will destabilize your computer for years to come