Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nohup is not writing log to output file

Tags:

python

nohup

People also ask

How do I write nohup output to a file?

If standard output is a terminal, the command's standard output is appended to the file 'nohup. out'; if that cannot be written to, it is appended to the file '$HOME/nohup. out'; and if that cannot be written to, the command is not run. But if I already have one command using nohup with output going to /nohup.

Where do nohup logs go?

Since there isn't a terminal to associate with it, nohup logs everything to an output file, nohup. out . By default, that file is located in whichever directory you started the command in. nohup.

Why nohup out is empty?

The short answer is to use nohup python -u args... to force python to print and flush buffer for each print() call. I believe this is due to Python's default behaviour to write to buffer first, but nohup doesn't trigger the buffer to be cleared.

What is nohup log?

nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.


You can run Python with the -u flag to avoid output buffering:

nohup python -u ./cmd.py > cmd.log &

It looks like you need to flush stdout periodically (e.g. sys.stdout.flush()). In my testing Python doesn't automatically do this even with print until the program exits.


  • Using -u with nohup worked for me. Using -u will force the stdout, stderr streams to be unbuffered. It will not affect stdin. Everything will be saved in "nohup.out " file. Like this-

    nohup python -u your_code.py &
    

    You can also save it into your directory. This way-

    nohup python -u your_code.py > your_directory/nohup.out &
    
  • Also, you can use PYTHONUNBUFFERED. If you set it to a non-empty string it will work same as the -u option. For using this run below commands before running python code.

    export PYTHONUNBUFFERED=1
    

    or

    export PYTHONUNBUFFERED=TRUE
    

P.S.- I will suggest using tools like cron-job to run things in the background and scheduled execution.


export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &

or

nohup python -u ./cmd.py > cmd.log &

https://docs.python.org/2/using/cmdline.html#cmdoption-u


Python 3.3 and above has a flush argument to print and this is the only method that worked for me.

print("number to train = " + str(num_train), flush=True)
print("Using {} evaluation batches".format(num_evals), flush=True)