Linux – Bash: better way to output to both console and output file than tee

bashbash-scriptingcommand linelinux

What I need to display is a log refreshing periodically. It's a block of about 10 lines of text. I'm using |tee and it works right now. However, the performance is less satisfying. It waits a while and then outputs several blocks of texts from multiple refreshes (especially when the program just starts, it takes quite a while to start displaying anything on the console and the first time I saw this, I thought the program was hanging). In addition, it breaks randomly in the middle of the last block, so it's quite ugly to present.

Is there a way to improve this? (Maybe output less each time and switch between output file and console more frequently?)

Update: Here is what's in my bash script right now:
tail -f /var/log/syslog | egrep --line-buffered "my search string" > tmp.txt & python script.py | tee result.log

Best Answer

I think your problem arises from a fundamental feature of pipes i.e. buffering.

The workarounds are messy but you should look at commands like unbuffer or script or stdbuf.

Perhaps something to stop output bufferring by tee like this:

 your_program | stdbuf -o0 tee

PS. I'm not at a console now so I cannot try this.

Related Question