Friday, October 29, 2021

[SOLVED] Can't seeing python script output in nohup.out file

Issue

I have a bash script that executes in loop a python script:

 while true; do python3 script.py && break; done

launched with nohup:

nohup ./run_script.sh &

Why if I run the command:

tail -f nohup.out

I don't see the output of the python script?

I correctly see the script in my running process:

pi        2757  1.5  3.4  37268 33064 ?        S    15:06   0:18 python3 script.py
pi        2819  0.0  0.0   1908   388 ?        S    15:07   0:00 /bin/sh ./run_script.sh
pi        2820  1.6  3.5  37268 33096 ?        S    15:07   0:18 python3 script.py

If I directly launch the python script with nohup, I see the output but I need to relaunch the script everytime it fails, so I need to lauch it with the bash script

Maybe I'm missing some concept concerning nohup usage.


Solution

I resolved modifing the script in this way:

while true; do python3 -u script.py && break; done

The output file nohup.out during execution remains empty until the execution is finished. This happens because of output buffering. Adding the -u flag I can avoid output buffering.

For other details see this blog page https://janakiev.com/blog/python-background/.



Answered By - DarkSkull