3

I found this at stackoverflow and it's code is this:-

@echo off
Set "MyProcess=calc.exe"
echo "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%">nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyProcess%")
pause 

But my problem is different. According to the Header, I want to check continually if a program is running. So i made MY batch file like this.

@echo off
:B
Set "MyProcess=cmd.exe"
echo "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "1.bat","" "2.bat", "" "3.bat")
goto B    

Which seems to be not working too.

And the hardest thing of them all, I am just a beginner at batch so i don't understand most of the things here

Thank You.

1
  • Instead of copying random code from the internet that you don't understand and trying to change it you should first do some learning ...
    – DavidPostill
    Commented Jan 4, 2016 at 13:31

1 Answer 1

1

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

4
  • Thank You for the answer @LMFAO_A_JOKE. The script now works as expected. Thanks for the resources too. very informative answer :-) You know what I asked the same question on stack overflow and I got downvoted to hell with no answer Commented Jan 4, 2016 at 10:27
  • Can you answer another question for me, Superuser has restricted me for posting a question within 2 days. And this question was also a part of it. The whole question is rather very long, and I got he answer to the first part there, the second part here and I want you to answer the third and last part here and I'll upvote both your answers @LMFAO_A_JOKE Commented Jan 4, 2016 at 10:33
  • Hey there @LMFAO_A_JOKE . I have posted the codes of all batch files i know, but there is a link to a website and the vbscript is there (at the end of the page). Here is the link and i'll update the question with the code of vbscript (not the link of it.) Commented Jan 4, 2016 at 11:59
  • Thanks @LMFAO_A_JOKE. It is 7:00 Pm here in India. So ill go to sleep after some time Commented Jan 4, 2016 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .