Friday, October 29, 2021

[SOLVED] Redirect nohup to stdout on a script

Issue

I have a .sh script that via nohup runs a lot of Python scripts.

I use nohup to run them in the background and avoid neccesary enter pressing when the "nohup: ignoring input and appending output to 'nohup.out'" message appears.

nohup python script2.py 2> /dev/null &
nohup python script2.py 2> /dev/null &
[...]

When I run this .sh, I get the two Python scripts running in the background, ok.

This Python scripts generate a log file, which I deriver to std.stdout:

stream_config = logging.StreamHandler(sys.stdout)  <-- here
stream_config.setFormatter(formatter)
importer_logger.addHandler(stream_config)  

Due to my implementation, I want to launch those nohup but sending the stdout to to PID 1

I can do this easily without using nohup as follows:

python tester.py > /proc/1/fd/1

But how can I combine that "don't press enter to continue" and "deriver the file to stdout"?

I tried these with no luck:

nohup python tester.py > /proc/1/fd/1 2> /dev/null &
nohup python tester.py 2> /proc/1/fd/1 &

Thanks in advance.


Solution

You can fix your command by using the shell to indirectly start your program so that you can redirect its output rather than the output of nohup:

nohup bash -c "exec python tester.py > /proc/1/fd/1" 2> /dev/null &

This isn't the best option but it at least corrects the issue that made your attempts fail.



Answered By - Dan D.