Check if a process is running or not continually, and start multiple programs if not and terminate itself
I want to check continually if a program is running
The below should work for the need to call the batch scripts one by one and basically just looks to confirm in memory on the machine the TASKLIST
command is run against to confirm it's running.
If it's not running, call the batch scripts one after the other; otherwise, go to the :B
routine and start the check over again. If it is running, then ECHO
the "%MyProcess%" est en cours d^'execution
and then go to the :B
routine and check again to confirm (a continual check).
WARNING: Be sure you have GOTO EOF at the end of the batch scripts that are called and NOT EXIT
or else they won't pass control back to the caller so the original batch can continue its loop checking. (see below example CALLED SCRIPT EXAMPLE).
SCRIPT THAT HAS CHECKING LOGIC (use this one)
@ECHO OFF
:B
SET MyProcess=cmd.exe
ECHO "%MyProcess%"
TASKLIST | FINDSTR /I "%MyProcess%"
IF ERRORLEVEL 1 (GOTO :StartScripts) ELSE (ECHO "%MyProcess%" est en cours d^'execution)
GOTO :B
:StartScripts
::: //-- Put in the full path to the batch scripts to call
::: //-- Be sure the security context this process runs as has access to execute the below called batch scripts
CALL "C:\Path\1.bat"
CALL "C:\Path\2.bat"
CALL "C:\Path\3.bat"
GOTO :B
(I assume the called batch scripts will have logic to start whatever process was determined to NOT be found running in memory on the machine the abobe script runs so just be sure the GOTO EOF
is the last thing ALL those do. I'd need to see the logic in these to know for myself if that will work fine or not though otherwise.)
CALLED SCRIPT EXAMPLE (e.g. 1.bat, 2.bat, 3.bat)
<LOGIC>
<LOGIC>
<LOGIC>
<LOGIC>
<LOGIC>
<LOGIC>
<LOGIC>
<END OF LOGIC>
::: Keep GOTO EOF here to pass control back to the caller
GOTO EOF
OTHER NOTES
You could put the logic in the same batch script that the three
separate called batch scripts run as well and just call those as
labels\routines but I'd need to see the logic in those to confirm
it'd work. As long as they each have the GOTO EOF
at the bottom of
them, they'd work the same way but this way all logic would be in the
same ONE batch script file.
The batch script logic with TASKLIST
and FINDSTR
as I have it to
should work just fine without the [/NH /FI imagename eq
] syntax
as in the example not working you showed in your question. Using
the FINDSTR
rather than FIND
as in your example is fine as
well.
I have logic with batch similiar to this that checks locally and remotely for processes running on servers that either auto-corrects sort of like you're doing or sends an email to indicate someone needs to give something some attention, so I've confirmed this method of the script logic I provided works with success. I just don't have all your logic to optimize, confirm, etc. otherwise.
ADDITIONAL RESOURCES