Wednesday, March 16, 2022

[SOLVED] Why is my nohup.out empty?

Issue

I have a simple python script that just runs an infinite while loop and prints "running". When I start it up with nohup in the background, I see a nohup.out file in my current directory but nothing ever gets written to it. I am confused. I have tried the following

nohup test.py 

writes to nohup.out but obviously does not run in the background

nohup test.py &

runs in background but does not write to nohup.out

nohup test.py 2>&1 &

also runs in the background but does not write to nohup.out

I bet I am missing something simple. Any ideas?

Here is my python script for reference:

import sys
from time import sleep

def main():
    print "Starting...."
    while True:
        print "running..."
        sleep(5.00)


if __name__ == "__main__":
     main()

Solution

Maybe your stdout is not flushed immediately, because it is not attached to a terminal. Try using

import sys
sys.stdout.flush()

to flush your output.

In python3 you can use flush=True as an argument to the print function to do that.

print("running...", flush=True)


Answered By - Karl Bartel
Answer Checked By - Clifford M. (WPSolving Volunteer)