21968

Modifying “… | tee -a out.txt” to stream output live, rather than on completion?

<h3>Question</h3>

I would need to output the output of a command on a file. Let's say my command is zip -r zip.zip directory , I would need to append/write (any of these options would be fine) to a file (let's say out.txt). I got zip zip.zip directory | tee -a out.txt so far, but it doesn't seem to work, it just writes the whole output when the command is over... How can I achieve this?

Thanks ;)


<h3>Answer1:</h3>
<h3>Background (ie. Why?)</h3>

Redirections are immediate -- when you run somecommand | tee -a out.txt, somecommand is set up with its stdout sent <em>directly</em> to a tee command, which is defined by its documentation to be unbuffered, and thus to write anything available on its input to its specified output sinks as quickly as possible. Similarly, somecommand >out.txt sets somecommand to be writing to out.txt literally <em>before</em> it's even started.

What's not immediate is flushing of buffered output.

That is to say: The standard C library, and most other tools/languages, <em>buffer</em> output on stdout, combining small writes into big ones. This is generally desirable, inasmuch as decreases the number of calls to and from kernel space ("context switches") in favor of doing a smaller number of more efficient, larger writes.

So your program isn't really waiting until it exits to write its output -- but it <em>is</em> waiting until its buffer (of maybe 32kb, or 64kb, or whatever) is full. If it never generates that much output at all, then it only gets flushed when closing the output stream.

<hr /><h3>Workarounds (How? -- GNU version)</h3>

If you're on a GNU platform, and your program is leaving its file descriptors the way it found them rather than trying to configure buffering <em>explicitly</em>, you can use the stdbuf command to configure buffering like so:

stdbuf -oL somecommand | tee -a out.txt

defines stdout (-o) to be line-buffered (L) when running somecommand.

<hr /><h3>Workarounds (How? -- Expect version)</h3>

Alternately, if you have expect installed, you can use the unbuffer helper it includes:

unbuffer somecommand | tee -a out.txt

...which will actually simulate a TTY (as expect does), getting the same non-buffered behavior you have when somecommand is connected directly to a console.


<h3>Answer2:</h3>

Did you try option command > out.log 2>&1 this log to file everything without displaying anything, everything will go straight to the file

来源:https://stackoverflow.com/questions/42938106/modifying-tee-a-out-txt-to-stream-output-live-rather-than-on-completio

Recommend

  • How to efficiently manage memory/time in C++?
  • Uninitialized Texture in OpenGL
  • Near matches not found in CONTAINSTABLE
  • Use element-resize-detector library in an Angular2 application
  • js: please explain this variable value with 5 double quotations
  • Add and extract struct from NSMutableArray [duplicate]
  • Cannot update/delete Firestore field with “period” in the name
  • How to exclude folders from c++ core guidelines checkers check path?
  • Determining Filetype with PHP. What is Magic Database?
  • Bash test if an argument exists
  • Why are const atomic variables not updated, but static atomic variables are?
  • How to use RubyGem Sanitize transformers to sanitize an unordered list into a comma seperated list?
  • Where to findViewById in a Drawer MainActivity?
  • Oracle SQL - get number of days between two dates for a specified month
  • How to stop Neo4j community edition
  • PHP Change XML node values
  • Select multiple fields with single group by in django
  • Why doesn't the tray icon context menu work for my RemoteApp?
  • .Net how to set IsReadOnly for a property for a usercontrol
  • Wildcard in MapPageRoute
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Identifying dates in strings using NLTK
  • Thick underline when hover AND when active
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice
  • Shrinking Bootstrap Navbar with logo on scroll
  • How can I do case insensitive string search with Linq and SQL Server?
  • How to move to lines with the same indentation in Visual Studio Code
  • How to create OLE Automation to be used with ClearExplorer
  • playing mp3 from nsbundle
  • Swift: UIView.animate works unexpectedly
  • How to create subsets of a single set of elements with XSLT?
  • Dynamic XML Schema Validates Subsection of Document
  • How convert html to BBcode in C#
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web