Logging a batch file

Asked

Viewed 933 times

0

I have the following problem:

We receive a daily backup of the database from 4AM to 5AM, and we have a batch job running at 6 AM to get this database running on a VM.

In Task Scheduler, everything is ok, and it runs every day, but we want to log the actions that the batch file does.

Is there any way to do that?

What I’ve already tried:

  1. Call the batch with another batch that writes output to a file. The scheduler runs the job indefinitely.
  2. Call a powershell script to run the batch and write to a file. Same problem as above
  3. Surround batch instructions with () and with it make it print the results of the instructions. >"arquivo" (instruções)

1 answer

3


1) Add a if for a conditional loop

2) Redirect a call to the bat itself "setting" the.log file..

3) add the 3 lines/codes below and at first of your file .bat already existing.


Update: To also save in same log possible/ mistakes

  • Change: "%~0" >>!_log! & exit /b

  • To: "%~0" >!_log! 2>&1 & exit /b


@echo off && setlocal enableextensions enabledelayedexpansion
if /i "!_log!/" == "/" (echo/Chamada do lote iniciada: !date! - / - !time!
set _log="%temp%\log_bat.log" && echo/>!_log! & "%~0" >>!_log! & exit /b) else (endlocal)
:: seu código atual em bat começa nesse ponto salvando as saídas em "%temp%\log_bat.log” ::

  • commenting:
@echo off && setlocal enableextensions enabledelayedexpansion

rem :: verifica se a variável recebeu valor :: 
if /i "!_log!/" == "/" (

   echo/Chamada do lote iniciada: !date! - / - !time!

   rem :: caso não tenha recebido algum valor, vai atribuir e chamar o próprio bat 
   set _log="%temp%\log_bat.log" && echo/>!_log! & "%~0" >>!_log! & exit /b

   rem :: caso tenha recebido algum valor, vai seguir a execução do bat, já salvando em: !_log! 
   ) else (endlocal)

:: seu código atual em bat começa nesse ponto salvando as saídas em 
 %temp%\log_bat.log” ::
  • Ended up solving the problem! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.