1

Running some shell scripts that run Python scripts, I get re-ordering of stdout lines when redirecting to tee or a file.

For example, if I run the shell script and get output to my stdout, my output looks like:

Line1: Starting X 
Line2: Output of X 
Line3: Starting Y 
Line4: Output of Y

But If I run the same script and redirect output to file, or use tee, print statements from the underlying Python script will be printed at the very end of execution.

Line1: Output of X
Line2: Output of Y
Line3: Starting X
Line4: Starting Y

Is there anything I can do to avoid this?

1

It could be that the Python script writes to both stdout and stderr, and you're redirecting only one of them (to a file or through tee).

To check what output goes where:

./myscript 2>/dev/null    # shows only stdout
./myscript >/dev/null     # shows only stderr

To redirect both:

./myscript >myfile 2>&1   # both to myfile
./myscript |& tee ...     # both through tee

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.