0

How can I save a command output to a file in real-time?

For example, if my command is python log.py where log.py contains:

import time
print('testing')
time.sleep(100) # sleeping for 100 seconds

and if I save the output with python log.py | tee command.log or python log.py > command.log, the command.log file typically won't contain the output of python log.py, namely testing, until it finishes executing. Instead, I don't want to have to wait ~100 seconds to be able to view testing in command.log.

  • 1
    The linked question doesn't explain it, but the C library buffers the output differently based on if the output goes to a terminal or to a file/pipe. Python appears to be similar to everything else in this. So we need some way to change the buffering. Of course it could be done within the Python program, too, if that's what you wanted. I interpreted the question as looking for a generic solution. – ilkkachu Jul 04 '18 at 20:31
  • @ilkkachu Thanks for the explanation. "the C library buffers the output differently based on if the output goes to a terminal or to a file/pipe" -> which C library? Also, when using unbuffer python log.py > command.log, does it still matter that the C library buffers the output differently based on if the output goes to a terminal or to a file/pipe? – Franck Dernoncourt Jul 04 '18 at 20:38
  • 1
    unbuffer and stdbuf basically override the default logic. Though in different ways, see: unbuffer or stdbuf for removing stdout buffering? – ilkkachu Jul 04 '18 at 20:41

0 Answers0