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
djm

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.

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 System SpecsSystem Spec
.

08 Sep 2011   #2
nighpher

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
pclove

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.

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 System SpecsSystem Spec
.


Reply

 Script to restart an application if it has stopped.




Thread Tools





Similar help and support threads
Thread Forum
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
application has stopped working error
Hi... I am working on 3ds max application, which always takes lot of time to load files or do some any complex work, the problem is windows 7 thinks it as "application has stopped working":devil: and closes that down, i am sure, if i can give more time to 3ds max application it will surely come...
BSOD Help and Support
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
Software
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...
Software
Application has stopped working...Win7 HP x64
Hello all, I have been attempting to load Future Pinball (Future Pinball) and also visual pinball for about 2 months now. I've submitted forum questions in 5 different places (my first one on this forum) asking for recommendations. I have updated graphics drivers, uninstalled and re-installed so...
BSOD Help and Support
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 08:19.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App