3

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.

2
  • Would you be able to insert calls to flush in your test program? Commented May 21, 2016 at 3:49
  • Can you post some of your code? Commented May 21, 2016 at 5:28

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.