Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my nohup.out empty?

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()
like image 833
Deep Kapadia Avatar asked Dec 16 '11 16:12

Deep Kapadia


People also ask

Where is nohup out saved?

By default, nohup runs a process in the foreground while redirecting the output to nohup. out file. The file is located in the current working directory unless the user does not have write permissions in that directory. In that case, the nohup.

What happens if you delete nohup out?

You can delete it if you don't want what's in it, but if the program that created the file is still running, then the disk space won't actually be reclaimed until the program exits.

Why has nohup stopped?

The 2>&1 redirects standard error to standard output, which you then capture in a file. If the command attempts to read from the terminal while in the background, it will be stopped.

What is a nohup out file?

Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup. out file is used as the default file for stdout and stderr.


1 Answers

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)
like image 83
Karl Bartel Avatar answered Sep 25 '22 05:09

Karl Bartel