Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending output of a Batch file To log file

Tags:

batch-file

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.

like image 849
Monojeet Nayak Avatar asked Dec 16 '10 07:12

Monojeet Nayak


People also ask

How do I append to a batch file?

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. Following is a simple example of how to create a file using the redirection command to append data to files.

How do you append the output of a command to a file in Windows?

If you want to save the output from multiple commands to a single file, use the >> operator instead. This appends the output of a command to the end of the specified file, if it already exists. If the file doesn't exist, it creates a new one.


2 Answers

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  
like image 107
Jon Skeet Avatar answered Sep 23 '22 20:09

Jon Skeet


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 1
  • timeout command 2

Example (1 second):

TIMEOUT /T 1000 /NOBREAK

Sleep:

  • sleep command (if sleep.exe is installed)

Example (1 second):

sleep -m 1000

Alternative methods:

  • Sleeping in a batch file
  • batch script, put to sleep until certain time

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

like image 25
JohnB Avatar answered Sep 22 '22 20:09

JohnB