1.

Solve : Capture error/and output of commands to log file?

Answer»

I'd like to be able to capture all error output generated from a dos batch file (regardless of error generated)
  i.e.

if errorlevel --1 goto error

:Error
capture error and pipe to error log file

Appreciate insightWell the easiest way to make sure to capture it all is to put

>>errorlog.txt

at the end of every line that you believe an error could be generated from. What commands in the batch file are you wanting to know about the errors for? Many times, you can write a batch file that can accomodate any errors that may arise with proper planning and the correct knowledge. If possible, post the file here and we can all analyze it and let you know where errors are likely to occur and what to do about them, and how to code it.Hi there, and thanks for the reply.  Here is my basic batch file which in turn call several other subroutines as part of a system environment refresh:

REM CPY Daily Refresh job-created Sept11

echo on
:Step 1--Call StopAppBat_Server.bat to shutdown PS environment
C:
CD c:\scripts\hr83cpy
ECHO Shutting Down App Batch Server
CALL StopAppBat_Server.bat
if %errorlevel%==1 goto ERROR



:Step 2--use osql to kill any active user sessions
CD c:\scripts\HR83CPY
osql -S Server1 -E -i c:\scripts\hr83cpy\uspkill.txt  >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
echo User sessions have been terminated!

osql -S Server1 -E -i c:\scripts\hr83cpy\uspkill.txt  >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
echo User sessions have been terminated!
if %errorlevel%==1 goto ERROR


:Step 3--check for existence and time stamp of backup file
CD c:\scripts\HR83CPY
osql -S Server1 -E -Q "exec master..uspVerifyBackupFiles '\\svmpsdb01\backup$\HR83PRD_Full_LiteSpeed.BAK', 4, 12"  >>c:\scripts\HR83CPY\hr83cpy_refresh.txt
if %errorlevel%==1 goto ERROR



:Step 4--use isql command to kick off refresh executing refresh script
CD c:\scripts\hr83cpy
echo Refreshing Database
isql -S Server1 -E -i c:\scripts\hr83cpy\hr83cpy-Restore.sql -o >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
if %errorlevel%==1 goto ERROR


:Step 4--Restart PS environment
REM c:
REM CD c:\scripts\hr83cpy
echo Restarting App And Batch Server
CALL Start_Tuxedo_Domain.bat
if %errorlevel%==1 goto ERROR


:Step 5--Send Refresh log
CD C:\Scripts\hr83cpy
echo Sending Refresh log file
CALL Refresh_Log.cmd
if %errorlevel%==1 goto ERROR

:Step 6 Move and rename log file
CD C:\Scripts\hr83cpy
move c:\scripts\hr83cpy\HR83CPY_refresh.txt c:\scripts\hr83cpy\logs\HR83CPY_refresh.txt
CALL file-rename.bat
if %errorlevel%==1 goto ERROR

:END

:ERROR
if %errorlevel%==1 CALL ERROR_EMAIL.bat


Please take a look.  Your suggestions are appreciated.
PeterSo form what I can tell, you want the entire program to run all the way through, and any errors that are generated to trigger the ERROR_EMAIL.bat. This isn't difficult, though does require some additional code.

Code: [Select]echo on
set errorcheck=0

:Step 1--Call StopAppBat_Server.bat to shutdown PS environment
C:
CD c:\scripts\hr83cpy
ECHO Shutting Down App Batch Server
CALL StopAppBat_Server.bat>temp.txt
if errorlevel 1 (
  set /p log=<temp.txt
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else del temp.txt

:Step 2--use osql to kill any active user sessions
CD c:\scripts\HR83CPY
osql -S Server1 -E -i c:\scripts\hr83cpy\uspkill.txt  >temp.txt
echo User sessions have been terminated!
set /p log=<temp.txt
if errorlevel 1 (
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else (
  echo %log% >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
  del temp.txt
)

:Step 3--check for existence and time stamp of backup file
CD c:\scripts\HR83CPY
osql -S Server1 -E -Q "exec master..uspVerifyBackupFiles '\\svmpsdb01\backup$\HR83PRD_Full_LiteSpeed.BAK', 4, 12"  >temp.txt
if errorlevel 1 (
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else (
  echo %log% >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
  del temp.txt
)

:Step 4--use isql command to kick off refresh executing refresh script
CD c:\scripts\hr83cpy
echo Refreshing Database
isql -S Server1 -E -i c:\scripts\hr83cpy\hr83cpy-Restore.sql -o >temp.txt
if errorlevel 1 (
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else (
  echo %log% >>c:\scripts\hr83cpy\hr83cpy_refresh.txt
  del temp.txt
)

:Step 5--Restart PS environment
REM c:
REM CD c:\scripts\hr83cpy
echo Restarting App And Batch Server
CALL Start_Tuxedo_Domain.bat >temp.txt
if errorlevel 1 (
  set /p log=<temp.txt
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else del temp.txt

:Step 6--Send Refresh log
CD C:\Scripts\hr83cpy
echo Sending Refresh log file
CALL Refresh_Log.cmd >temp.txt
if errorlevel 1 (
  set /p log=<temp.txt
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else del temp.txt

:Step 7 Move and rename log file
CD C:\Scripts\hr83cpy
move c:\scripts\hr83cpy\HR83CPY_refresh.txt c:\scripts\hr83cpy\logs\HR83CPY_refresh.txt
CALL file-rename.bat >temp.bat
if errorlevel 1 (
  set /p log=<temp.txt
  echo %log% >>errorlog.txt
  set errorcheck=1
  del temp.txt
) else del temp.txt

if "%errorcheck%"=="1" call ERROR_EMAIL.bat
:END
You will have some problems when using the call command if the other programs require additional inputs or output anything. If that is the case with any of the called programs, you will NEED to exchange the
set /p log=<temp.txt
with
for /f "delims=" %%A in (temp.txt) do set log=%%A
which will then capture the list line of the file (which is where any error message should be.) Please post if you run into anything that you don't know what to make of it, and we can try to see if there is something else we can do. Like I stated, it's the fact that this program is calling other programs that is causing the biggest issue. Is there anyway you could include them within this one program? Or are they used by other programs as well?Thanks for your reply and modified script provided.

Right now I am trying out the script as posted.  I did run into one issue where I had the wrong database Server (Step 3&4), and it did write the error message to errorlog.txt.  That is fine, but ideally would like ERROR_EMAIL.bat to email me this when the issue had occured, somehow it didn't get triggered.

ERROR_EMAIL.bat :
echo off
postie -host:mailserver -to:[email protected] -from:[email protected] -s:"Refresh Error" -msg:"Check logs, EP88RPT Refresh failed**log file attached" -a:"C:\scripts\EP88RPT\errorlog.txt"

Also I did try to run the script bundled together with most of the sub routines as shown below (without the modified error logic):
echo on
:Step 1--Call StopAppBat_Server.bat to shutdown PS environment


:Step1
:CREATE batch_stop_log.txt file with current date and time
echo off
if not [%1]==[] goto start
set var=%date% %time%
set var=%var:/=-%
>"EP88RPT_refresh.txt" echo %var%
cmd /c %0 start >>"EP88RPT_refresh.txt"
goto :EOF
:start

:Step 2a
:Set Environment variables, shut down Application Server, delete :cache, and logs directory and recreate logs directory. 
SET APPSERVER=EP88RPT
SET DATABASE=EP88RPT
echo Shutting down %DATABASE% Application Server...
echo.
ECHO Shutting down Application Server
d:
CHDIR D:\psoft\%APPSERVER%\appserv
psadmin -c shutdown! -d %DATABASE%
psadmin -c shutdown! -d %DATABASE%
psadmin -c shutdown! -d %DATABASE%
psadmin -c shutdown! -d %DATABASE%
if %errorlevel%==1 goto ERROR
ECHO DELETING System Cache
rmdir /S /Q D:\psoft\%APPSERVER%\appserv\%DATABASE%\CACHE
if %errorlevel%==1 goto ERROR
ECHO Deleted App Server logs
rmdir /S /Q D:\psoft\%APPSERVER%\appserv\%DATABASE%\LOGS
if %errorlevel%==1 goto ERROR
mkdir D:\psoft\%APPSERVER%\appserv\%DATABASE%\LOGS
if %errorlevel%==1 goto ERROR


:Step 2b
:Set Environment variables, shut down Process Scheduler Server, delete cache, :and drop logs directory and recreate logs directory. 

:start
SET PROCESS_SCHEDULER=EP88RPT
SET DATABASE=EP88RPT
echo Shutting down %DATABASE% Process Scheduler...
echo.
d:
CHDIR D:\psoft\%PROCESS_SCHEDULER%\appserv
psadmin.exe -p stop -d %DATABASE%
psadmin.exe -p stop -d %DATABASE%
psadmin.exe -p stop -d %DATABASE%
psadmin.exe -p stop -d %DATABASE%
if %errorlevel%==1 goto ERROR

echo Deleting System Cache
rmdir /S /Q D:\psoft\%PROCESS_SCHEDULER%\appserv\prcs\%DATABASE%\CACHE
if %errorlevel%==1 goto ERROR

echo %DATABASE% Batch Cache Deleted
rmdir /S /Q D:\psoft\%PROCESS_SCHEDULER%\appserv\prcs\%DATABASE%\LOGS\
if %errorlevel%==1 goto ERROR

mkdir D:\psoft\%PROCESS_SCHEDULER%\appserv\prcs\%DATABASE%\LOGS\
if %errorlevel%==1 goto ERROR

ECHO %DATABASE% Deleted Process Schedulers Logs
ECHO Delete Log_Output files
rmdir /S /Q D:\psoft\%PROCESS_SCHEDULER%\appserv\prcs\%DATABASE%\log_output
ECHO Log_output deleted!
pause

:Step 3--use osql to kill any active user sessions
c:
CD c:\scripts\ep88rpt
osql -S Server1 sa -P password -i c:\scripts\ep88rpt\uspkill.txt  >>c:\scripts\ep88rpt\ep88rpt_refresh.txt
echo User sessions have been terminated!
if %errorlevel%==1 goto ERROR
pause
c:
CD c:\scripts\ep88rpt
osql -S Server1 -U sa -P password -i c:\scripts\ep88rpt\uspkill.txt  >>c:\scripts\ep88rpt\ep88rpt_refresh.txt
echo User sessions have been terminated!
if %errorlevel%==1 goto ERROR
pause


:Step 4--check for existence and time stamp of backup file
c:
CD c:\scripts\ep88rpt
osql -S Server1 -U sa -P password -Q "exec master..uspVerifyBackupFiles '\\svmpsdb03\backup$\PA880PRD_Full_LiteSpeed.BAK', 4, 12"  >>c:\Scripts\ep88rpt\ep88rpt_refresh.txt
if %errorlevel%==1 goto ERROR


:Step 5--use isql command to kick off refresh executing refresh script
c:
CD c:\scripts\ep88rpt
echo Refreshing Database
isql -S Server1 -U sa -P password -i c:\scripts\ep88rpt\ep88rpt-Restore.sql -o >>c:\scripts\ep88rpt\ep88rpt_refresh.txt
if %errorlevel%==1 goto ERROR


:Step 6--Restart PS environment
:Create batch_stop_log.txt file with current date and time
echo off
if not [%1]==[] goto start
set var=%date% %time%
set var=%var:/=-%
>>"EP88RPT_refresh.txt" echo %var%
cmd /c %0 start >>"EP88RPT_refresh.txt"
goto :EOF
:start
:Set Environment variables, boot Application Server
SET APPSERVER=EP88RPT
SET DATABASE=EP88RPT
echo Booting up %APPSERVER% Application Server...
echo.
ECHO Booting up Application Server
d:
CHDIR D:\psoft\%APPSERVER%\appserv
psadmin -c boot -d %DATABASE%
if %errorlevel%==1 goto ERROR

ECHO Application Server has started!



:ECHO Booting up Process Scheduler
d:
CHDIR D:\psoft\%APPSERVER%\appserv
psadmin.exe -p start -d %DATABASE%
if %errorlevel%==1 goto ERROR

ECHO PRocess Scheduler has started!


:Step 7--Send Refresh log
c:
CD C:\Scripts\ep88rpt
echo Sending Refresh log file
echo off
postie -host:mailserver -to:[email protected] -from:[email protected] -s:"EP88RPT has been refreshed" -msg:"EP88RPT has been refreshed**log file attached" -a:"C:\scripts\ep88rpt\EP88RPT_refresh.txt"

:Step 8 Move and rename log file
c:
CD C:\Scripts\ep88rpt
move c:\scripts\ep88rpt\ep88rpt_refresh.txt c:\scripts\ep88rpt\logs\ep88rpt_refresh.txt
echo off
CD logs

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "EP88RPT_refresh.txt" EP88RPT_refresh_%%e-%%f-%%g.txt

:END

:ERROR
if %errorlevel%==1 CALL ERROR_EMAIL.bat

REM ECHO Error Log Open >C:\Scripts\ep88rpt\ERR.LOG
REM ECHO Error 1 >>C:\Scripts\ep88rpt\ERR.LOG
REM CALL ERROR_EMAIL.bat


When executing this script together it fails at step 3
I get "The process cannot access the file because it is being used by another process."

So 2 things that would be great.  First if the script will email me on error with the error log file, and second if we go with just the one large main script can we make it work based on the error I got in the above paragraph?

Much appreciated for your help/insight.

In order to address your first issue regarding the ERROR_EMAIL.bat, I would suggest either checking your syntax on postie, or possibly using a different program to accomplish the task. You might also look at using CD to ensure you are in the correct directory when the program runs.

On your second issue, this error is common and is usually rectified by using taskkill to make sure the task is not running prior to trying to running the osql line.

In your script, to save some time and carriage returns, try using the CD /D when changing directories and don't worry about the seperate drive letter change. Also, I see a lot of repetitiveness in the file. Is this needed? When four lines say exactly the same thing ( psadmin -c shutdown! -d %DATABASE% ) what is the purpose behind it? Are they performing different functions?Hey there...

Thanks for all your help so far.  I did make an error on the isq/osq syntax and managed to run the script ok.  What I did NOTICE was when I ran step 2/3/4 (sql tasks) they would not echo over the output to the EP88RPT_refresh.txt.  When  I ran step 2, the task was echoed into the temp.txt, but not added to the EP88RPT_refresh.txt.  This is the typical reply from sql when runnning step 2 "1> 2> Killing Users
-----------------
Done" 

Here's part of the screen when I ran just this step from the command prompt on 2 lines I got:

C:\Scripts\EP88RPT>set /p log= 02> was unexpected at this time.

C:\Scripts\EP88RPT>  echo 1> 2> Killing Users >>errorlog.txt

Similar issue with step 3 where the output was written to temp.txt and not added to EP88RPT_refresh.txt.
C:\Scripts\EP88RPT>osql -S ZVMPSESQL03 -U sa -P 29HollyBurn$ -Q "exec master..u
pVerifyBackupFiles '\\svmpsdb01\backup$\HR83PRD_Full_LiteSpeed.BAK', 4, 12"   1
temp.txt
2> was unexpected at this time.

C:\Scripts\EP88RPT>  echo 1> 2> Killing Users >>errorlog.txt

One additional note on your question for the 4 times using the psadmin; its to isure the environment is complely shutdown as there can be issues with some threads remaining open.

Hope this helps.  Getting closer.
Did not know that the output would use those redirect ">"s. Also, looking back over the script I posted, I'm seeing that I did not add the set /p log=<temp.txt line before the errorlevel check. Looks like I missed it in step 3 and 4.

To take care of the issue with redirection characters, I would suggest parsing the output. REPLACE the set /p log=<temp.txt line on steps that involve the sql commands with:
Code: [Select]for /f "tokens=1,2* delims=>" %%A in (temp.txt) do (set log=%%A %%B %%C)
This should take out all of the redirection symbols that are causing problems. This is of course assuming that the output is fairly standard and only has two >'s that show up. Please let me know if the output from the sql commands has varying numbers of > output, as that will take a different type of coding to get through.Thanks for this, I have been pretty busy and hope to try this out near the end of the week.  Let you know how it goes.



Discussion

No Comment Found