Search code examples
linuxstdoutstderr

Linux stdout stderr redirect to file: different from terminal output


I'm having trouble with a bash script that is indended to repeatedly execute a program (not my own) and append the output from each execution to a text-file.

#!/bin/bash
for run in {1..100}
  make prog >> log.txt 2>&1
done

So far so good. The problem is that after executing this, the content of log.txt is very different from the terminal-output the program normally produces. The program's output alternates between stdout and stderr so that on the terminal it might look something like this:

stdout- stderr- stdout- stderr - ...

and so on. Now in the text file instead of this, there are larger 'chunks' of stdout and stderr messages lumped together. E.g. there might be 10 lines of stdout-output at the beginning of the file followed by 10 lines of stderr-output and so forth.

I suppose this is some sort of buffering problem. How do I get around this?

Any form of help would be appreciated.


Solution

  • Turning off output buffering might help. Both the unbuffer command and the stdbuf command can manipulate buffer sizes, e.g., like so: stdbuf -o0 -e0 yourcommandhere