51

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.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
Monojeet Nayak
  • 721
  • 1
  • 7
  • 13

4 Answers4

118

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 
Dalek Control
  • 587
  • 5
  • 7
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, ">>" worked.....also can you please tell me what lines should i add to my batch file which will make it execute after every 30mins.....along with the Timestamp! – Monojeet Nayak Dec 16 '10 at 07:13
  • 1
    @Monojeet: You probably shouldn't do that in your batch file - instead, use Task Scheduler. – Jon Skeet Dec 16 '10 at 07:27
  • Actually this thing is going to be deployed accross many servers, so not possible to configure task scheduler on every server, a script will be handy! – Monojeet Nayak Dec 16 '10 at 07:35
  • 3
    @Monojeet: Well you're basically talking about writing your own scheduler as a script... which doesn't sound like a good idea to me. I'm sure it's possible to remotely configure scheduled tasks, and that would be a better way to go, IMO. If you *really* want to avoid that, I'd put the scheduling in a "proper" program (whether that's Java, a C# wrapper or whatever) rather than trying to do it in a batch file. – Jon Skeet Dec 16 '10 at 07:39
  • 1
    @Monojeet: You could always make your batch file *invoke* the Windows Task Scheduler, of course, using "at". – Jon Skeet Dec 16 '10 at 07:39
  • "javaprog %1 %2 >>test.log".....how can i add a timestamp(date and time) to the log file... – Monojeet Nayak Dec 16 '10 at 07:44
  • @Monojeet: Well, you could run "date /t >> test.log" and "time /t >> test.log" beforehand, assuming you can't change the Java code. – Jon Skeet Dec 16 '10 at 07:49
  • @John:- yeah , i shud have easily done that if i cud change my code..i have already written the code and checked in :) ...basically its a monitoring java prog which returns certain error codes for a infrastructure...i need to call this java prog at certain intervals( 3mins) so that the monitoring team can pick the error codes and send notifications...Simple!! I have done this thing in Unix...just used the sleep command in the wrapper script....but dont know how do i do that in windows! – Monojeet Nayak Dec 16 '10 at 07:55
  • @Monojeet: Just because you've checked in the code doesn't mean you can't change it any further... I'd hope, anyway. Otherwise it had better be *perfect* to start with... On Unix I'd use cron rather than sleeping, for a monitoring program. – Jon Skeet Dec 16 '10 at 08:00
  • @John:- ok , will change the code, was just trying to learn it through the batch file....Thanks a lot for you reply....i saw your profile...it was an honor speaking to you...cheers! – Monojeet Nayak Dec 16 '10 at 08:47
4

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

Community
  • 1
  • 1
JohnB
  • 18,046
  • 16
  • 98
  • 110
0

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.

Jon Mitchell
  • 217
  • 6
  • 16
0

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
Jordan Stewart
  • 3,187
  • 3
  • 25
  • 37