Error logic recomendation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
netwerkassist
Posts: 7
Joined: 21 Apr 2010 12:03

Error logic recomendation

#1 Post by netwerkassist » 11 Oct 2011 13:22

I have a batch file that will shutdown an application, refresh its database and restart the application. I'm looking for some recommendations as to the best way to handle error logic on it. I was thinking about capturing all error details and send it to a error.log file, but not sure the best way to proceed.

Right now I use the if %errorlevel%==1 goto ERROR condition, believing that any error would then call another batchfile to send an error email out.

Here is the script I have to date:

@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
c:
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


Thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Error logic recomendation

#2 Post by Ed Dyreen » 13 Oct 2011 01:47

'
my GOD, :mrgreen:

Code: Select all

@echo off &SetLocal EnableExtensions EnableDelayedExpansion

:: Define an error handler
::--------------------------------------------------------------------------------------------------------------------------
set "@Handle_ErrorLevel=if ^!errorlevel^! neq 0 call :§Handle_ErrorLevel ^!errorlevel^! "This line here""
goto :skippy ( The Kangeroo :)
:§Handle_ErrorLevel ()
::(
   set /a $error = %~1
   echo.
   echo. [error:%$error%] %~2
   echo. what to do, o master ?
   pause
   exit /b %$error%
::
exit /b %$error%
::)
:skippy "()"
::--------------------------------------------------------------------------------------------------------------------------

echo.This instruction is fine
%@Handle_ErrorLevel%
This wrong is echo instruction ?
%@Handle_ErrorLevel%

EndLocal
Untested !

You can also send stderr to an "Error.LOG" for later inspection etc..

Post Reply