13

I have to finish a writeup of a few coreutils commands for a course at the moment, and I can't think of a starting point for a small practical code example that demonstrates the potential uses of stdbuf. Has anyone used it to fix the interaction of a couple specific unix commands? I know what it does. It's just that the first commands that came to mind have their own buffering controls, and normal terminal output is line buffered anyway. It must be popular for appending to logs, yet I can't find a good command to demonstrate there.

In the case of nohup, are there any commands that are commonly run with it to prevent interruption?

As I mentioned, I am working on this for a course assignment at the moment. This however doesn't violate any of its rules. I'm just trying to find a good starting point for these examples. I don't have one for stdbuf, and I dislike the rudimentary one I was using for nohup.

2
  • For stdbuf: normal terminal output is line-buffered anyway - very true, which makes it really annoying when you want line-buffered output for a pipeline of 3 or more connected processes. Like <infile grep1 | sort | grep3 or <infile nl -bp... | sort -nk1,1 because nl doesn't have any line-buffering options.
    – mikeserv
    Mar 27, 2015 at 22:44
  • 2
    These two utilities have nothing to do with each other, so you should ask these two questions separately. For nohup, why don't you like the presentation on Wikipedia? Mar 27, 2015 at 23:23

3 Answers 3

11

stdbuf examples at http://www.pixelbeat.org/programming/stdio_buffering/

nohup is used for any long running command that you want left running across logins. You can also do this with screen(1) or retroactively with screen + https://github.com/nelhage/reptyr

1
  • Yeah I actually read that page on stdio buffering. Prior to the other answer it was just difficult to imagine a simple example. I was unaware that nohup persists across logins, and I couldn't think of many long running processes outside of daemons, which are typically launched by startup scripts.
    – kav
    Mar 28, 2015 at 3:08
4

Let's start with an example without stdbuf. You need 2 terminals because tail -f will block.

Terminal 1

touch log_file1
tail -f log_file1 | cut -d " " -f 1 | uniq

Terminal 2

echo "A 1" >> log_file1

You don't see any output in Terminal 1.

Let's try again and add stdbuf to Terminal 1.

touch log_file2
tail -f log_file2 | stdbuf -oL cut -d " " -f 1 | uniq

Terminal 2

echo "A 1" >> log_file2

This time you should see A in Terminal 1.

0

Here is a concrete example

stdbuf -oL ./a.out > log.txt   

Only when stdbuf is used, the printout can be piped into file.

1
  • 3
    Please include the relevant details from the linked question; a.out could be anything, and your answer on its own doesn’t explain why stdbuf is needed. Feb 17, 2021 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .