bash Nohup 附加输出和错误而不是覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10169789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nohup appending output and error instead of overwrite
提问by shujaat
I am using nohup to append the java output and error to the same log file. The problem is it writes an output and then it overwrites the log file for error and output is erased.
我正在使用 nohup 将 java 输出和错误附加到同一个日志文件中。问题是它写了一个输出,然后它会覆盖错误的日志文件并且输出被删除。
The nohup command is
nohup 命令是
nohup java Daemon 1000 >logs/wrapper.log 2>logs/wrapper.log &
This is the message I want to log in wrapper.log from the Daemon.java
这是我想从 Daemon.java 登录 wrapper.log 的消息
System.out.println("This is output that should go to the file");
System.err.println("This is error that should go to the file");
But only the last message is written in the file. The most reasonable answer is to know how to append the wrapper.log for outputs and errors and not the overwrite
但只有最后一条消息写入文件。最合理的答案是知道如何为输出和错误附加 wrapper.log 而不是覆盖
Any ideas
有任何想法吗
Thanks
谢谢
回答by ormaaj
The most portable (and my preferred method) is:
最便携(也是我首选的方法)是:
cmd >>logs/wrapper.log 2>&1 &
the >>FDredirect opens with the O_APPEND flag. cello's answer is a bashism (and most kshes and zsh) to redirect both stdout and stderr at once, but doesn't solve the problem of opening in append mode.
该>>FD重定向与O_APPEND标志打开。大提琴的答案是一种 bashism(以及大多数 kshes 和 zsh)同时重定向 stdout 和 stderr,但并没有解决在追加模式下打开的问题。
See: http://mywiki.wooledge.org/BashPitfalls#somecmd_2.3E.261_.3Elogfileand associated links.
请参阅:http: //mywiki.wooledge.org/BashPitfalls#somecmd_2.3E.261_.3Elogfile和相关链接。
EDIT: I see this doesn't actually address the append problem. I'll edit the page. The links are still relevant.
编辑:我看到这实际上并没有解决追加问题。我会编辑页面。这些链接仍然相关。
回答by cello
most likely, the error-out overwrites the standard-out, as you both write to the same file.
try something like:
nohup java Daemon 1000 &> logs/wrapper.log &
最有可能的是,错误输出会覆盖标准输出,因为你们都写入同一个文件。尝试类似:
nohup java Daemon 1000 &> logs/wrapper.log &
&>works on some shells (bash in any case, not sure about tcsh/zsh/...), so try it out or let us know which shell you are using.
&>适用于某些 shell(无论如何都是 bash,不确定 tcsh/zsh/...),因此请尝试一下或让我们知道您使用的是哪个 shell。

