Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write script output to file and command-line?

I have a long-running Python script that I run from the command-line. The script writes progress messages and results to the standard output. I want to capture everything the script write to the standard output in a file, but also see it on the command line. Alternatively, I want the output to go to the file immediately, so I can use tail to view the progress. I have tried this:

python MyLongRunngingScript.py | tee log.txt

But it does not produce any output (just running the script produces output as expected). Can anyone propose a simple solution? I am using Mac OS X 10.6.4.

Edit I am using print for output in my script.

like image 316
Björn Pollex Avatar asked Sep 26 '10 13:09

Björn Pollex


1 Answers

You are on the right path but the problem is python buffering the output.

Fortunately there is a way to tell it not to buffer output:

python -u MyLongRunngingScript.py | tee log.txt
like image 70
Imre L Avatar answered Sep 30 '22 06:09

Imre L