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 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.

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
Thread Forum
Access Denied (5) message on Shutdown or Restart script
I have two shortcuts on my desktop Shutdown shortcut C:\Windows\System32\shutdown.exe /f /s /t 0 And Restart Shortcut C:\Windows\System32\shutdown.exe /r /f /t 0 I noticed today that when I try to run either one, I get this message: Access is Denied.(5) This is something new. Anyone familiar...
General Discussion
MFC application has stopped working
I'm running BlazeDTV 6.0 and when trying to scan channels I get the error message: "MainApp MFC Application has stopped working". Any thoughts?
Music, Pictures & Video
my application has stopped working
my application sage 50 that was running fine in past but now it is not running and when i click on it windows gives the error "sage 50 has stopped working". i used different fixes as i searched and read on internet but useless.plz suggest me better solution.thanks
Don't want shutdown script to run on restart
Hi, I've got my system configured via gpedit so that it runs a custom batch script when it shuts down. The script can take about a minute to complete because it is telling some external hardware to run through its power down procedure. However when I tell the system to _reboot_ the script...
Restart time script problem
I was using the restart script from the above stickied reboot time post. It was working fine now all of a sudden it says Can't Fine script engine "VBScript" for script I havnt done anything but uninstall mcafee and install comodo firewall with avast anti virus. Under associate programs it says...
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 04:20.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App