17

Im new to python and web development, I have a basic issue I believe.
I'm running my server with pyramid, and I use nohup.out to write output to a file.

nohup ../bin/pserve development.ini  

When I do tail -f nohup.out
I can see all the output coming from the logging.info() calls in my code.
but I don't see all the output from the print() calls.

what is the reason for that, and how can I set it that I will see the print() in the nohup file?

3 Answers 3

25

You can use

nohup python -u python_script.py &
2
  • 2
    -u: force the binary I/O layers of stdout and stderr to be unbuffered; stdin is always buffered; text I/O layer will be line-buffered; also PYTHONUNBUFFERED=x
    – Logan Yang
    Aug 31, 2019 at 2:55
  • python -u works for me while stdbuf -oL -eL python does not Jan 6, 2021 at 7:12
11

You can use stdbuf -oL to flush the print statements. Command will look like

nohup stdbuf -oL python python_script.py > nohup.out &
2
  • Thanks for your helpful answer, but I think your command needs to be ended with a & right?
    – lleiou
    Mar 26, 2018 at 4:49
  • This does not work. Tested on CentOS 7.4 with Python 3.7.6. The behavior is the same whether we use stdbuf -oL or not.
    – jdhao
    Jan 25, 2022 at 3:43
4

Printed output is buffered when not to a terminal. nohup replaces stdout with a non-terminal (a file, in fact).

In the absence of any code, all I can do is guess, but the most likely answer is that your output is buffered inside the python process, and when that buffer fills it will be flushed to nohup.out. To see if this is the case, try adding a lot more prints to fill up the buffer faster.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.