1

I am trying to redirect the output of certain program with threads to certain file.

I have tried running command > file 2&>1 but if command has not stopped running, file will still be empty.

Is there any way to asynchronously dump the output to file?

Example python snippet:

import _thread

def run():
    raise ValueError('plah')

_thread.start_new_thread(run, ())
while True:
    pass

Running python program.py 2> log.txt does not yield an output in log.txt until the program is manually stopped.

countermode
  • 7,533
  • 5
  • 31
  • 58
Lezkus
  • 13

2 Answers2

2

Make the output unbuffered. A simple way to do this with Python is to set PYTHONUNBUFFERED:

PYTHONUNBUFFERED=1 python program.py 2>log.txt
Satō Katsura
  • 13,368
  • 2
  • 31
  • 50
1

Python buffers output by default. Give the -u flag, or put PYTHONUNBUFFERED=yes to avoid that.