Usually I'd like to run my python code in linux as the following:
nohup python test.py > nohup.txt 2>&1 &
in the file test.py, I often use print
to print out some messages to stdout. But actually I have to wait very long time then could see the messages were printed out to nohup.txt. How can I make it print out quickly.
You could call flush
on stdout
. If it is possible and practical for you to adjust your code to flush your buffers after the print
call, in test.py
:
from sys import stdout
from time import sleep
def log():
while True:
print('Test log message')
# flush your buffer
stdout.flush()
sleep(1)
While running this logging test, you can check the nohup.txt
file and see the messages being printed out in realtime.
just make sure you have coreutils
installed
stdbuf -oL nohup python test.py > nohup.txt 2>&1 &
this just sets buffering to off for this command ... you should see immediate output
(you might need nohup before stdbuf ... im not sure exactly)
alternatively ... just put at the top of test.py
import sys
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.