nohup bash myscript.sh > log.log yeilds empty file until the process is stopped manually

Viewed 32

I have a python file that I run using a .sh file providing some args. When I run this file on the terminal using bash myscript.sh it runs fine, prints progress on the console. When I use nohup like nohup bash myscript.sh > log.log nothing gets saved to log.log file but the processes are running (it's a 4 GPU process and I can see the GPU usage in top and nvidia-smi). As soon as I kill the process using either ctrl+c or kill command, all the output gets printed to the file at once along with keyboard interrupt or process killed message. I have tried nohup myscript.sh &> log.log nohup myscript.sh &>> log.log but the issue remains the same. What's the reason for such a behaviour?

myscript.sh runs a python file somewhat like

python main.py --arg1 val1 --arg2 val2

I tried

python -u main.py

but it doesn't help. I know the script is working fine as it occupies exactly same amount of memory as it should.

1 Answers

Use stdbuf:

nohup stdbuf -oL bash myscript.sh > log.log

Your problem is related to output buffering. In general, non-interactive output to files tend to be block buffered. The -oL changes output buffering to line mode. There is also the less efficient -o0 to change it to unbuffered.

Related