【问题标题】:How to redirect prints to log file on nohup如何将打印重定向到 nohup 上的日志文件
【发布时间】:2018-01-28 17:48:31
【问题描述】:
nohup python3 main.py > log.output &

因此,我的框架得到了一些输出,但我的个人print 语句没有被记录到log.output。有没有什么办法解决这一问题?

nohup 下的输出

nohup: ignoring input * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 571-306-491 XXX - - [28/Jan/2018 17:56:42] "POST /TestEndpoint HTTP/1.1" 201 - XXX - - [28/Jan/2018 17:57:00] "POST /TestEndpoint HTTP/1.1" 201 -

正常运行输出

python3 main.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 571-306-491
test endpoint
XXX - - [28/Jan/2018 18:01:52] "POST /TestEndpoint HTTP/1.1" 201 -

尝试了所有这些:

nohup python3 -u main.py &
nohup python3 main.py > log.output 2>&1 &
nohup python3 -u main.py > log.output 2>&1 &
nohup python3 main.py > log.output &
nohup python3 -u main.py > log.output &

只有这显示正确的输出python3 main.py

【问题讨论】:

标签: python


【解决方案1】:

print 默认情况下将数据发送到 sys.stdout,但除非您另有明确说明,否则它会被缓冲。

也可以尝试将stderr 重定向到stdout,只是为了检查 nohup 是否以某种方式影响它。

nohup python3 main.py > log.output 2>&1 &

【讨论】:

  • 仍然没有得到打印语句,每个 POST 之间应该有一个打印语句,它们没有出现。如果我在前台运行它而不使用 nohup / & ,它们会显示得很好。我编辑了主帖子以显示日志输出
  • 可能有一个 nohup.out 文件,所有打印的行都被重定向到,并且位于您运行此命令时所在的同一文件夹中
  • 我也试过了,nohup python3 -u main.py & 将输出放到 nohup.out 并且也不包含打印
  • print('...', flush=True)?打电话给print怎么样
  • 感谢作品......但我有大量的印刷品。没有比在每条打印语句中添加flush=True 更好的解决方案吗?
猜你喜欢
  • 2011-01-31
  • 2015-01-01
  • 2012-10-13
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2011-11-01
相关资源
最近更新 更多