What's Wrong with Tee Command?

I am running the following python script piped to tee command

#!/usr/bin/python

from sys import exit,exc_info
from time import sleep

try:
        print "hello"
        #raise KeyboardInterrupt
        while True:
                print "hello"
                sleep(1)

except KeyboardInterrupt:
        print "Key board Interrupt"
        exit(0)

Let's say I stored this in file.py

Now if I execute:

./file.py | tee somefile

Now press Ctrl+C, observe nothing is printed into the somefile and stdout

Under normal execution:

./file.py

Upon Ctrl+C:

 hello
 hello
 ^CKey board Interrupt

Also file redirection is working fine. What's wrong with tee

2 Answers

Nothing's wrong with tee. Python buffers output if it detects it's not writing to a TTY. See this Unix & Linux post. Use sys.stdout.flush() to force flushing the buffer.

1

You can use this way as well:

python -u file.py | tee somefile

This problem mentioned on stackoverflow

Your Answer

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

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest