0

I have a command that slowly outputs a list. I want this list to be saved on a file and also see it slowly being generated on the terminal.

python script.py 2>&1 | tee File.txt

This does not work for me. While the command is saved, I don't see the list of websites appearing on the terminal.

Skyleft
  • 35
  • 8

2 Answers2

1

By default stdout is line buffered when going to a terminal, but uses a larger buffer when being redirected, hence tee and the terminal don't see the output until later.

For ways to get script.py to not buffer the output see the answers to this question Disable output buffering

For example if script.py is:

#!/usr/bin/python3
import time
for i in range(5):
    print('This is line', i, flush=True)
    time.sleep(1)

Running ./script.py | tee File.txt will print each line to the terminal as the line is executed - one second apart.

If you remove flush=True then the entire output is buffered, and nothing is printed until the script finishes 5 seconds later when everything is printed.

2>&1 redirects stderr to stdout, so you may need to apply the same buffering to stderr as well as stdout.

Kim
  • 409
  • 3
  • 9
0

Per the Linux Documentation Project (TLDP),

2>&1 # Redirects stderr to stdout. # Error messages get sent to same place as standard output.

And,

&>filename # Redirect both stdout and stderr to file "filename."

So to pipe both to a file,

Command &> | tee File.txt

Or just stdout,

Command | tee File.txt
oxr463
  • 1,573
  • 3
  • 14
  • 34
  • None of these work either, they simply don't write anything on the terminal. I recall a long time ago I might've had this same issue and someone told me to just add a "-letter" to the command to refresh the terminal, but I don't exactly remember what it was. – Skyleft Jun 11 '19 at 03:46