I have a program that can crash and need to schedule a task which restarts it when the program is no longer running.
The problem I have is there are duplicate process names so I cannot just rely upon that, (there may be others still running) outside of my target process.
I have identified that the process in question is a child process of explorer.exe parent. I have the below code that would deal with situation and identify the correct process name, however I can only run basic tasks like kill process etc.
How would I utilize the below but instead of the actual last line of this code to kill the process I need to check if the child process is running and if not start the program from provided patch.
@echo off
setlocal enabledelayedexpansion
set "ParentProcessName=explorer.exe"
set "ChildProcessName=myprocessname.exe"
set ParentProcessIdCount=0
for /f "usebackq tokens=2 delims==" %%A in (`wmic process where "name='%ParentProcessName%'" get processid /format:list`) do (
if "%%A" neq "" (
set /a ParentProcessIdCount+=1
set ParentProcessId!ParentProcessIdCount!=%%A
)
)
echo DEBUG: Found %ParentProcessIdCount% possible parent processes.
if %ParentProcessIdCount% == 0 goto :eof
set "conditions=parentprocessid=%ParentProcessId1%"
for /l %%A in (2,1,%ParentProcessIdCount%) do (
set "conditions=!conditions! or parentprocessid=!ParentProcessId%%A!"
)
set "conditions=(%conditions%) and name='%ChildProcessName%'"
wmic process where "%conditions%" call terminate
I think I’ve tried all I could do with my limited knowledge.
BartelJannsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.