Stdout Buffer
After a long time, I decided to post again since I needed just one small information; however, it took some of my time. The information is not about C, but unix shell.
I was running a Python script, which just checks the status of some URLs and prints their index or any error if exists. Since the number of URLs was large, I wanted to see the progress on the console. Additionally, I needed to know erroneous URLs if any. However, I didn’t do that saving/logging in Python. I wanted to handle it in the terminal.
In order to print to the terminal and a file at the same time, I have used Tee:
./script.py | tee output.txt
In the above line, the output of script is piped (redirecting output one program to another) to tee, which prints to stdout (the console (actually, the file descriptor for non-error output) and the file output.txt at the same time. Additionally, I combined stderr (the file descriptor for error output) with stdout in the following line:
./script.py 2>&1 | tee output.txt
1 and 2, respectively, represent stderr and stdout. 2>&1 combines stderr with stdout.
After typing the last command, it should have worked. However, I couldn’t see any output in the terminal. Then, I learned from the Internet that it is due to the buffer. So that I know the reason, how I can force buffer of stdout not to keep anything? I found the answer again online:
stdbuf -o 0 ./script.py 2>&1 | tee output.txt
stdbuf is a command, which can set the buffer settings of stdin (input stream), stdout, and stderr. With the parameter -o, I set the buffer size of stdout to 0 when I’m running the script. Then, the output of the script appeared on the terminal without any delay.
On some websites (including the first answer of the reference), it is mentioned that unbuffer from the package expect is suggested to stop buffering. However, I do not think that it is necessary to install another tool for such a one-line thing.
References:
http://stackoverflow.com/a/11337109/1080580

















