Durga Prasanth
Durga Prasanth

Reputation: 44

Batch script to find if program is not responding and then start another application else exit batch job

I want a batch script that checks if the program is in running or not responding state. If it is in running state exit the batch script, else kill the program start the other application.

I tried using the following code but it is executing opening application even though the program is in running state. Please help me in finding a solution

@echo off
TASKLIST/IM jusched.exe /FI "STATUS eq NOT RESPONDING">nul /T /F  && start "" notepad.exe

Upvotes: 2

Views: 1221

Answers (1)

Compo
Compo

Reputation: 38719

You appear to be mixing up the TaskKill and TaskList syntax. (Enter TaskList/? and TaskKill/? at the command prompt for usage information). Also I think TaskList output is always successful so it would probably need to be checked using something which does register if unsuccessful, e.g. Find.exe.

Here is a basic step by step example for you.

@Echo Off
Rem Setting name of target process
Set "TP=jusched.exe"
Rem Setting name of new process
Set "NP=notepad.exe"
Rem Setting TaskList filters to reduce line length
Set "F1=ImageName eq %TP%"
Set "F2=Status eq Not Responding"
Rem Check target process status and exit if no match
TaskList /FI "%F1" /FI "%F2%" /NH|Find /I "%TP%">Nul||Exit/B
Rem Kill unresponsive process and wait a little
TaskKill /IM "%TP%" /T /F>Nul 2>&1&&Timeout 3 /NoBreak>Nul
Rem Open new process
Start "" "%NP%"

Upvotes: 2

Related Questions