Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Script to restart an application if it has stopped.

27 Dec 2010   #1

Windows 7 Professional
Script to restart an application if it has stopped.

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.

ECHO Restart Application

SETLOCAL EnableExtensions

REM Enter the application information.
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"

IF {%StartOnlyIfKilled%}=={1} (
	IF {%Success%}=={0} GOTO End
	DATE /t > %LogFile%
	TIME /t > %LogFile%
	ECHO "SRM killed." > %LogFile%

ECHO Restarting %AppName%...
START "%ExeFile%" "%ExePath%\%ExeFile%"

IF EXIST %KillStatus% DEL /F /Q %KillStatus%


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


My System SpecsSystem Spec

08 Sep 2011   #2

Windows XP 32 bit

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 System SpecsSystem Spec
27 Feb 2012   #3

7 Ultimate 7601

I made this script for the Gadget Sidebar in Windows 7. 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.

echo ==========================================
echo  Simple Script to Restart Sidebar Gadgets
echo  Created by
echo ==========================================
echo STEP 1: Closing Sidebar . . .
TASKKILL /F /IM sidebar.exe
echo STEP 2: Starting Sidebar . . .
start sidebar.exe
echo SUCCESS: Sidebar is now up.
You may be able to change the program exe to yours.
My System SpecsSystem Spec


 Script to restart an application if it has stopped.

Thread Tools

Similar help and support threads for2: Script to restart an application if it has stopped.
Thread Forum
application has stopped working error BSOD Help and Support
Don't want shutdown script to run on restart Software
Application has stopped working...Win7 HP x64 BSOD Help and Support
Restart time script problem Performance & Maintenance

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 05:56 PM.
Twitter Facebook Google+

Windows 7 Forums

Seven Forums Android App Seven Forums IOS App

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33