tee output not appearing until cmd finishes
Asked Answered
T

1

6

Usually if I want to print the output of a command and in addition capture that output in a file, tee is the solution. But I'm making a script using a utility which seems to have a special behaviour. It's the wps wireless assessment tool bully.

If I run a bully command as normal (without tee), the output is shown in a standard way, step by step. But If I put the pipe at the end to log like this | tee "/path/to/my/logfile" the output on the screen freezes. It shows nothing until the command ends. And after ending, it shows everything in one shot (not step by step) and of course it puts the output also in the log tee file.

An example of bully command: bully wlan0mon -b 00:11:22:33:44:55 -c 8 -L -F -B -v 3 -p 12345670 | tee /root/Desktop/log.txt

Why? Not sure if it only happens with bully or if there are other programs with the same behaviour.

Is there another way to capture the output into a file having the output on screen in real time?

Ticon answered 7/12, 2016 at 20:12 Comment(2)
Possible reason: bully changes output buffering to fully buffered when stdout is not connected to a terminal (such as a pipe).Brabble
See How to make output of any shell command unbuffered?.Laclos
S
8

What you're seeing is full buffering vs line buffering. By default, when stdout is writing to a tty (i.e. interactive) you'll have line buffering, vs the default of being fully buffered otherwise. You can see the setvbuf(3) man page for a more detailed explanation.

Some commands offer an option to force line buffering (e.g. GNU grep has --line-buffered). But that sort of option is not widely available.

Another option is to use something like expect's unbuffer command if you want to be able to see the output more interactively (at the cost of depending on expect, of course).

Suwannee answered 7/12, 2016 at 20:26 Comment(3)
Yeah, using unbuffer in front of the command did the trick... now it shows the output on screen step by step and saves to the file using "line buffering", so my command is: unbuffer bully wlan0mon -b 00:11:22:33:44:55 -c 8 -L -F -B -v 3 -p 12345670 | tee /root/Desktop/log.txtTicon
to install unbuffer on macos this was helpful apple.stackexchange.com/questions/193138/…Likable
I ran into this in Python, so I thought I'd share that you can use python -u to unbuffer the output. See docs.python.org/3/using/cmdline.html#cmdoption-uMonostylous

© 2022 - 2024 — McMap. All rights reserved.