将批处理文件的输出附加到日志文件

发布于 2024-10-08 01:33:26 字数 120 浏览 0 评论 0原文

我有一个调用java程序的批处理文件。

输出被重定向到同一目录中的日志文件。 但是,每次运行批处理文件时,日志文件都会被替换...

我想将旧输出保留在日志文件中,并始终将新输出附加到日志文件中。

I have a batch file which calls a java program.

The output is redirected to a log file in the same directory.
However the log file is replaced everytime the batch file is run...

I would like to keep the old outputs in the log file and always append the new output to the log file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ繥欢鉨o。 2024-10-15 01:33:26

而不是使用“>”像这样重定向:

java Foo > log

使用“>>”将正常的“stdout”输出附加到新的或现有的文件:

java Foo >> log

但是,如果您还想捕获“stderr”错误(例如为什么 Java 程序无法启动),您还应该使用“2>&” 1”标签将“stderr”(“2”)重定向到“stdout”(“1”)。例如:

java Foo >> log 2>&1 

Instead of using ">" to redirect like this:

java Foo > log

use ">>" to append normal "stdout" output to a new or existing file:

java Foo >> log

However, if you also want to capture "stderr" errors (such as why the Java program couldn't be started), you should also use the "2>&1" tag which redirects "stderr" (the "2") to "stdout" (the "1"). For example:

java Foo >> log 2>&1 
心舞飞扬 2024-10-15 01:33:26

这不是您最初问题的答案:“将批处理文件的输出附加到日志文件?”

作为参考,这是您的后续问题的答案:“我应该添加哪些行我的批处理文件将使其每 30 分钟执行一次?”

(但我会接受 Jon Skeet 的建议:“您可能不应该在批处理文件中执行此操作 - 相反,请使用任务计划程序。”)

超时:

  • < a href="https://stackoverflow.com/questions/166044/sleeping-in-a-batch-file/5483958#5483958">超时命令1
  • 超时命令2

示例(1 秒):

TIMEOUT /T 1000 /NOBREAK

睡眠:

示例(1 秒):

sleep -m 1000

替代方法:

这是您的第二个后续问题的答案:“随着时间戳?”

在批处理文件中创建日期和时间戳

示例:

echo *** 日期:%DATE:/=-% 和时间:%TIME::=-% *** >>>输出.log

This is not an answer to your original question: "Appending output of a Batch file To log file?"

For reference, it's an answer to your followup question: "What lines should i add to my batch file which will make it execute after every 30mins?"

(But I would take Jon Skeet's advice: "You probably shouldn't do that in your batch file - instead, use Task Scheduler.")

Timeout:

Example (1 second):

TIMEOUT /T 1000 /NOBREAK

Sleep:

Example (1 second):

sleep -m 1000

Alternative methods:

Here's an answer to your 2nd followup question: "Along with the Timestamp?"

Create a date and time stamp in your batch files

Example:

echo *** Date: %DATE:/=-% and Time:%TIME::=-% *** >> output.log

冬天旳寂寞 2024-10-15 01:33:26

请在您的 java 程序中使用 log4j。然后,您可以输出到多种媒体,创建滚动日志等,并包括时间戳、类名和行号。

Use log4j in your java program instead. Then you can output to multiple media, create rolling logs, etc. and include timestamps, class names and line numbers.

半暖夏伤 2024-10-15 01:33:26

也可以使用 java Foo | tee -a some.log。它也只打印到标准输出。喜欢:

user at Computer in ~
$ echo "hi" | tee -a foo.txt
hi

user at Computer in ~
$ echo "hello" | tee -a foo.txt
hello

user at Computer in ~
$ cat foo.txt
hi
hello

It's also possible to use java Foo | tee -a some.log. it just prints to stdout as well. Like:

user at Computer in ~
$ echo "hi" | tee -a foo.txt
hi

user at Computer in ~
$ echo "hello" | tee -a foo.txt
hello

user at Computer in ~
$ cat foo.txt
hi
hello
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文