Conditional section of batch file wont write to log files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Conditional section of batch file wont write to log files

#1 Post by SIMMS7400 » 21 Mar 2016 14:20

Hi Folks -

I have a batch script I'm using for a shut down procedure but recently had to add a section to check for hung processes. However, When I want to write any ECHO's in that section to my %logfile%, it wont.

The section labeled Check for Hung ESSSVR Processes at %TIME% is where my echo's wont write to log files. Any help is greatly appreciated, thanks!!

Code: Select all

REM --Call Environment Script--
call C:\Hyperion_Batch\Scripts\batch\_env.cmd

REM --Set working directory as script path--
cd /d %~dp0

REM ------------------------------------------------------------------------
REM SET LOG & ERROR PATHS
REM ------------------------------------------------------------------------

SET intrapath=%MAINPATH%%LOGPATH%%EPMSERVICESLOGS%
SET errorintrapath=%MAINPATH%%ERRORPATH%%EPMSERVICESLOGS%

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

REM ------------------------------------------------------------------------
REM SET & DELETE LOG FILES
REM ------------------------------------------------------------------------

SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log

if exist %logfile% del %logfile%
if exist %errorfile% del %errorfile%

REM -- Invoke DRM Batch Client
echo ********************************************************>>%logfile%
echo %~n0 at %TIME%                                  >>%logfile%
echo ********************************************************>>%logfile%

echo ********************************************************>>%logfile%
echo STOP ALL EPM SERVICES %TIME%                         >>%logfile%
echo ********************************************************>>%logfile%

REM Call %STOPEPMSYSTEM%

SET myError1=%errorlevel%
IF %myError1%==0 goto Stop_DRM_Service

echo ********************************************************>>%logfile%
echo Error Encountered in STOP ALL EPM SERVICES            >>%logfile%
echo ********************************************************>>%logfile%

goto AbnormalExit

:Stop_DRM_Service
echo ********************************************************>>%logfile%
echo STOP DRM SERVICE %TIME%                            >>%logfile%
echo ********************************************************>>%logfile%

REM Call %STOPDRMSERVICE%

SET myError2=%errorlevel%
IF %myError2%==0 goto Check_ESSSVR

echo ********************************************************>>%logfile%
echo Error Encountered in STOP DRM SERVICE               >>%logfile%
echo ********************************************************>>%logfile%

goto AbnormalExit

:Check_ESSSVR
echo ********************************************************>>%logfile%
echo Check for hung ESSSVR Processes %TIME%                  >>%logfile%
echo ********************************************************>>%logfile%


setlocal
tasklist /fi "imagename eq ESSSVR.exe" | find /i "ESSSVR.exe" > nul

if errorlevel 1 (
   set ProcExists=NO
) else (
   set ProcExists=YES
)
if /i "%ProcExists%"=="YES" (
   %PSKILL% /t "ESSSVR.exe"
) else (
        goto :ESSSVR_Not_Running
)

goto ESSSVR_Running

:ESSSVR_Not_Running
echo ********************************************************>>%logfile%
echo ESSSVR.exe WAS NOT RUNNING at %TIME%                    >>%logfile%        
echo ********************************************************>>%logfile%
goto NormalExit
PAUSE

:ESSSVR_Running
echo ********************************************************>>%logfile%
echo ESSSVR.exe WAS TERMINATED at %TIME%                  >>%logfile% 
echo ********************************************************>>%logfile%
goto NormalExit
PAUSE

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Conditional section of batch file wont write to log files

#2 Post by foxidrive » 21 Mar 2016 23:24

Which lines are not being echoed into the log file?

Pasting a large wad of script needs a little more description.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Conditional section of batch file wont write to log files

#3 Post by ShadowThief » 22 Mar 2016 00:27

Do all the other lines get echoed to the log file?

Also, you may want to get rid of that setlocal that's seemingly there for no reason.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Conditional section of batch file wont write to log files

#4 Post by miskox » 22 Mar 2016 02:32

ShadowThief wrote:Do all the other lines get echoed to the log file?

Also, you may want to get rid of that setlocal that's seemingly there for no reason.


This might be the reason. This setlocal erases the variable 'logfile' and it is displayed on the screen instead. Maybe you could add

Code: Select all

echo LOGFILE=%logfile%_


Saso

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Conditional section of batch file wont write to log files

#5 Post by SIMMS7400 » 22 Mar 2016 07:16

Hi Team -

Thank you for your replies!

Let me remove setlocal and see what that does. Also, ALL other log files are getting written to except the ones following Check for hung ESSSVR processes.

Thanks, ill report back!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Conditional section of batch file wont write to log files

#6 Post by foxidrive » 22 Mar 2016 19:24

miskox wrote:This setlocal erases the variable 'logfile' and it is displayed on the screen instead.


Setlocal doesn't erase existing variables, it keeps them all as they are.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Conditional section of batch file wont write to log files

#7 Post by foxidrive » 22 Mar 2016 19:32

SIMMS7400 wrote:The section labeled Check for Hung ESSSVR Processes at %TIME% is where my echo's wont write to log files.


Your code generates this here. There's no "at" in the line you mention, but it all works fine.


Code: Select all

********************************************************
z at 12:30:08.39                                 
********************************************************
********************************************************
STOP ALL EPM SERVICES 12:30:08.39                         
********************************************************
********************************************************
STOP DRM SERVICE 12:30:08.40                           
********************************************************
********************************************************
Check for hung ESSSVR Processes 12:30:08.41                 
********************************************************
********************************************************
ESSSVR.exe WAS NOT RUNNING at 12:30:08.66                   
********************************************************

Post Reply