I have the following script on an XP computer and it successfully detects whether an application has stopped or is not responding. It kills if required and restarts. But it doesn't work on Windows 7.
How can I either fix, or do the same thing another way on Windows 7?
On XP if the app is not running then "%TEMP%\TaskStatus.tmp.txt" ends up empty. On windows 7 it ends up with the string "INFO: ...." in it. So checking for size 0 doesn't work. I tried doing the same thing as done lower in the script but checking for INFO: instead of SUCCESS: but it doesn't work for some reason.
Code:
@ECHO OFF
ECHO Restart Application
ECHO.
ECHO.
SETLOCAL EnableExtensions
REM Enter the application information.
SET AppName=SRM
SET ExeFile=SRM.exe
SET ExePath=C:\sr\SRM\BIN
REM Select the conditions to kill the application.
REM A value of 1 = Yes, 0 = No
SET KillIfRunning=0
SET KillIfNotResponding=1
SET KillIfUnknownStatus=1
REM Specify when to start the application:
REM 1 = Start only if the process was previous killed.
REM 0 = Start the application regardless.
SET StartOnlyIfKilled=1
SET StartIfNotRunning=1
REM Ignores StartOnlyIfKilled
SET TaskStatus="%TEMP%\TaskStatus.tmp.txt"
SET KillStatus="%TEMP%\KillStatus.tmp.txt"
SET LogFile="c:\sr\scripts\SRMProcess.txt"
SET Success=0
IF {%StartIfNotRunning%}=={1} (
TASKLIST /FI "imagename eq %ExeFile%" > %TaskStatus%
FOR %%A IN (%TaskStatus%) DO if %%~zA == 0 (
DATE /t > %LogFile%
TIME /t > %LogFile%
ECHO "SRM not running." > %LogFile%
GOTO Restart
)
)
ECHO Killing existing %AppName% instance...
IF {%KillIfRunning%}=={1} CALL :CheckKillStatus "%ExeFile%" "RUNNING"
IF {%KillIfNotResponding%}=={1} CALL :CheckKillStatus "%ExeFile%" "NOT RESPONDING"
IF {%KillIfUnknownStatus%}=={1} CALL :CheckKillStatus "%ExeFile%" "UNKNOWN"
ECHO.
IF {%StartOnlyIfKilled%}=={1} (
IF {%Success%}=={0} GOTO End
DATE /t > %LogFile%
TIME /t > %LogFile%
ECHO "SRM killed." > %LogFile%
)
:Restart
ECHO Restarting %AppName%...
START "%ExeFile%" "%ExePath%\%ExeFile%"
ECHO.
IF EXIST %KillStatus% DEL /F /Q %KillStatus%
ENDLOCAL
:CheckKillStatus
ECHO Killing with status: %~2
TASKKILL /FI "STATUS eq %~2" /IM "%~1" /F > %KillStatus%
SET /P KillResult= < %KillStatus%
FOR /F "tokens=1,* delims=:" %%A IN ("%KillResult%") DO (
ECHO %%A:%%B
IF /I {%%A}=={SUCCESS} SET /A Success=%Success%+1
)
:End