-1

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?)

tmp
  • 7
  • 3
  • Note that there's a pretty good chance this question would really work better on [SU]. Still, it's not exactly off-topic here, so don't worry too much just yet. – Nathan Tuggy Mar 13 '15 at 01:37
  • I doubt there's much you can do apart from modifying the program that produces the output: see [this](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin), for example. – mustaccio Mar 13 '15 at 02:07
  • Please clarify how `tee` receives its input here, i.e., how log changes are being detected and output. – mklement0 Mar 13 '15 at 12:04
  • Sounds like STDOUT buffering is causing issues. [Check this other question](http://stackoverflow.com/q/11337041/477563) for possible workarounds. – Mr. Llama Mar 13 '15 at 16:17

2 Answers2

0

Assuming you can monitor the log as a file directly [update: turned out not to be the case]:

The usual way of monitoring a [log] file for new lines is to use tail -f, which - from what I can tell - prints new data added to the log file as it is being added, without buffering.

Similarly, tee passes data it receives via stdin on without buffering.

Thus, you should be able to combine the two:

 tail -f logFile | tee newLogEntriesFile
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Solved by flushing stdout after printing each block. Credit to Kenneth L! https://superuser.com/questions/889019/bash-better-way-to-output-to-both-console-and-output-file-than-tee

Community
  • 1
  • 1
tmp
  • 7
  • 3
  • Generally, if you solved your own problem, mark your _own_ answer as accepted, so future readers know what worked. However, please provide more detail on how you solved the problem; there's no "Kenneth L" mentioned on this page (users sometimes change their names). I assume it was one of the _comments_ that provided the crucial clue, but note that people restricted themselves to quick comments, because you didn't provide enough detail in your _question_, either (who's producing the output, and how? a file being appended to? a program you control? ...). – mklement0 Mar 15 '15 at 21:45
  • Please note that StackExchange doesn't allow you to accept your own answer until one day later and forgive me that I didn't set an alarm and actively countdown 24 hours to mark this question as answered. – tmp Mar 16 '15 at 15:51
  • Thanks for coming back to accept your answer (I'd forgotten about the 24-hour rule) and for providing more detail. Since it's still only _implied_, let me state it explicitly: Your log source is a program _you_ control and you were able to modify it to flush the stdout buffer as needed. – mklement0 Mar 16 '15 at 16:21