简体   繁体   English

将nohup重定向到脚本上的stdout

[英]Redirect nohup to stdout on a script

I have a .sh script that via nohup runs a lot of Python scripts. 我有一个.sh脚本,它通过nohup运行许多Python脚本。

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在后台运行它们,并避免neccesary 进入时,按“nohup的:忽略输入和输出追加到‘的nohup.out’”消息。

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. 运行此.sh ,我在后台运行了两个Python脚本,确定。

This Python scripts generate a log file, which I deriver to std.stdout : 这个Python脚本会生成一个日志文件,该文件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 由于我的实现,我想启动这些nohup但将stdout发送到PID 1

I can do this easily without using nohup as follows: 我可以轻松地做到这一点,而无需使用nohup ,如下所示:

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 "? 但是如何将“ 不要按Enter继续 ”和“ 将文件导出到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. 提前致谢。

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: 您可以通过使用Shell间接启动程序来修复命令,以便可以重定向其输出而不是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. 这不是最佳选择,但至少可以纠正导致尝试失败的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM