将批处理文件的输出附加到日志文件
我有一个调用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是使用“>”像这样重定向:
使用“>>”将正常的“stdout”输出附加到新的或现有的文件:
但是,如果您还想捕获“stderr”错误(例如为什么 Java 程序无法启动),您还应该使用“2>&” 1”标签将“stderr”(“2”)重定向到“stdout”(“1”)。例如:
Instead of using ">" to redirect like this:
use ">>" to append normal "stdout" output to a new or existing file:
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:
这不是您最初问题的答案:“将批处理文件的输出附加到日志文件?”
作为参考,这是您的后续问题的答案:“我应该添加哪些行我的批处理文件将使其每 30 分钟执行一次?”
(但我会接受 Jon Skeet 的建议:“您可能不应该在批处理文件中执行此操作 - 相反,请使用任务计划程序。”)
超时:
超时
命令1超时
命令2示例(1 秒):
TIMEOUT /T 1000 /NOBREAK
睡眠:
sleep
命令(如果安装了 sleep.exe)示例(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:
timeout
command 1timeout
command 2Example (1 second):
TIMEOUT /T 1000 /NOBREAK
Sleep:
sleep
command (if sleep.exe is installed)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
请在您的 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.
也可以使用 java Foo | tee -a some.log。它也只打印到标准输出。喜欢:
It's also possible to use
java Foo | tee -a some.log
. it just prints to stdout as well. Like: