【问题标题】:Redirect nohup to stdout on a script将 nohup 重定向到脚本上的标准输出
【发布时间】:2019-05-30 12:53:08
【问题描述】:

我有一个 .sh 脚本,它通过 nohup 运行大量 Python 脚本。

我使用nohup 在后台运行它们并避免在“nohup: ignoring input and appending output to 'nohup.out'”时按下必要的enter出现消息。

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

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

此 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 &

提前致谢。

【问题讨论】:

    标签: python linux unix nohup


    【解决方案1】:

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

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

    这不是最好的选择,但至少可以纠正导致您尝试失败的问题。

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多