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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like