在脚本上将 nohup 重定向到标准输出

Redirect nohup to stdout on a script

我有一个 .sh 脚本,通过 nohup 运行 很多 Python 脚本。

我在后台使用 nohup 到 运行 它们并避免在“nohup:忽略输入和将输出附加到 'nohup.out'”消息出现。

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

当我 运行 这个 .sh 时,我在后台得到两个 Python 脚本 运行ning,好的。

这个 Python 脚本生成一个日志文件,我导出到 std.stdout:

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

由于我的实施,我想启动那些 nohup 但将标准输出发送到 PID 1

我可以在不使用 nohup 的情况下轻松做到这一点,如下所示:

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

但是我怎样才能将“不要按回车键继续”和“将文件导出到标准输出”呢?

我尝试了这些但没有成功:

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

提前致谢。

您可以通过使用 shell 间接启动您的程序来修复您的命令,以便您可以重定向其输出而不是 nohup 的输出:

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

这不是最佳选择,但它至少纠正了导致您尝试失败的问题。