1

I'm running a program with large runtime and large output, so for logging while seeing the output I'm using the tee command on windows cmd.exe:

./program | tee -a program.log

the program prints live output (line after line with noticeable time gap), but the tee prints outputs only after termination of program. Is there any way to use tee with live output?

Note: The problem also persists on windows powershell.exe

  • 4
    When people say "Command Prompt" in Windows, they're referring to `cmd.exe`. What you're using is `powershell.exe` which is a different shell, also on Windows (although, now on all platforms). What does your code look like? Your question is currently too broad to be helped. – Maximilian Burszley Jun 25 '18 at 18:56
  • Are you sure that this is not `program` issue. For example, it detects that it no longer connected to real console and use full buffered output instead of line buffered or unbuffered? https://stackoverflow.com/q/31152759 – user4003407 Jun 26 '18 at 03:46

2 Answers2

7

Tee-Object doesn't flush the output stream itself. It waits for the interpreter to do it, and the interpreter often waits awhile. This is by design.

I would propose a work around like:

./program | ForEach-Object {
    Write-Host $_
    $_
} | Set-Content program.log

If you're still having problems, try:

./program | ForEach-Object {
    [Console]::WriteLine($_)
    [Console]::Out.Flush()
    $_
} | Set-Content program.log

That should be identical to Write-Host, but offhand I'm not sure how often that flushes to console.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • This question was closed, but this answer actually gave me exactly what I needed for this, so I question the sanity of the crowed here. – A. Wilson Nov 17 '18 at 07:54
  • @A.Wilson Closed questions this old are kept around because they're still useful. The issue here was the question is poorly written, so even though I got the right answer it's going to be hard to find with a search engine. – Bacon Bits Nov 17 '18 at 22:15
0

While it is a little unclear what you are doing, it sounds like you may be able to use a redirect to an output log file rather than using tee.

I'm not familiar with how you are using your AI training scripts but the following may work by sending both the stderr and stout to the same file. Then you could tail the file with the PowerShell command "Get-Content output.log –Wait" or another method of your choosing.

python_script args 1> output.log 2>&1

See https://support.microsoft.com/en-us/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout for description.