Appending output of a Batch file To log file

后端 未结 4 1313
暖寄归人
暖寄归人 2020-12-07 23:10

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 fi

相关标签:
4条回答
  • 2020-12-07 23:23

    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.

    0 讨论(0)
  • 2020-12-07 23:26

    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
    
    0 讨论(0)
  • 2020-12-07 23:41

    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 
    
    0 讨论(0)
  • 2020-12-07 23:46

    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

    0 讨论(0)
提交回复
热议问题