Script to restart an application if it has stopped.

djm

New member
Local time
3:58 PM
Messages
1
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
 

My Computer My Computer

OS
Windows 7 Professional
Try setting the script to run, or start running it with ADMINISTRATOR rights enabled.
9 times out of ten, the problem with getting an application in windows 7 to run properly is it needs to be run with administrative rights.

Also, since that script was originally for XP try running it in compatibly mode for XP or an earlier version.
 

My Computer My Computer

OS
Windows XP 32 bit
I made this script for the Gadget Sidebar in Win7. Once in a while, I get a couple of gadgets that disappear randomly. I believe the culprit is when I close the lid of my lappy, but I'm not absolutely sure.

Code:
@ECHO OFF
echo.
echo.
echo ==========================================
echo  Simple Script to Restart Sidebar Gadgets
echo.
echo  Created by pclove.us
echo ==========================================
echo.
echo.
echo STEP 1: Closing Sidebar . . .
echo.
TASKKILL /F /IM sidebar.exe
echo.
echo.
echo STEP 2: Starting Sidebar . . .
start sidebar.exe
echo.
echo SUCCESS: Sidebar is now up.
echo.
echo.
PAUSE

You may be able to change the program exe to yours.
 

My Computer My Computer

Computer Manufacturer/Model Number
Asus G50V
OS
7 Ultimate 7601
CPU
Core Duo 2.4GHz
Memory
6GB DDR2 667
Graphics Card(s)
nVidia GeForce 9700m GT
Screen Resolution
1366 x 768
Hard Drives
RAIDed 320GB 7K2 RPM Hitachi & 200GB 7K2 RPM Seagate
Back
Top