I'm running a python script that for some reason gets killed and because of it, the stdout is not printed. I know I can get the buffered stdout with sys.stdout.flush()
. However, since I don't know when the script is killed, I can miss the time between the last flush and the actual process ending. For example, is the following code only "test 1" will be saved before the 60s sleep ends. If I kill the program before that my log file will contain only the "test 1" print.
import sys,time
sys.stdout = open('log', 'w')
print("test 1")
sys.stdout.flush()
print("test 2")
time.sleep(60)
How can I catch the last stdout before the script is killed? How can I get the "test 2" print even if I kill the script before the 60s sleep ends?