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 somefileNow press Ctrl+C, observe nothing is printed into the somefile and stdout
Under normal execution:
./file.pyUpon Ctrl+C:
hello hello ^CKey board InterruptAlso 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.
You can use this way as well:
python -u file.py | tee somefileThis problem mentioned on stackoverflow