brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: stdbuf -oL makes a command in the middle of a pipe print each line right away

Linux
~/tips/tip-linux-stdbuf-ol-line-buffering

tail -f access.log | cut -d' ' -f1 | uniq

Stop a pipeline from holding back output until a buffer of several kilobytes is full.

You follow a log through a few filters and nothing appears, even though lines are coming in. The program in the middle is not broken; it is buffering its output because it writes to a pipe instead of a terminal.

Most programs built on the C library flush their output after every line when they write to a terminal, and only after a full block of several kilobytes when they write to a pipe. In a chain like this, the middle command sits on its output:

tail -f access.log | cut -d' ' -f1 | uniq

stdbuf starts a command with different buffering settings:

tail -f access.log | stdbuf -oL cut -d' ' -f1 | uniq

-oL sets standard output to line buffered, so cut passes on every line as soon as it is complete. You only need it on the commands that are not last in the pipe, since the last one writes to your terminal anyway.

Some tools have their own switch for this, which I prefer when it exists: grep --line-buffered, sed -u and python3 -u.

stdbuf works by loading a small library that changes the C library defaults when the program starts. That means it does nothing for programs that set their own buffering or are not linked against the C library, such as most Go binaries and anything statically linked. If adding stdbuf changes nothing, check whether the program has its own flush option.

Further reading: stdbuf(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link