0

I would like to write a batch script, and part of the script I want to check if program1.exe is still running. Once program1.exe is no longer running, I want to do something.

I can write a while loop to do this, but it will keep checking over and over and use up 100% of one of my cores in my CPU.

Is there an efficient way to check? Maybe a hack that checks this condition once a second?

Thanks

user3240815
  • 21
  • 1
  • 5
  • possible duplicate of [How to wait for a process to terminate to execute another process in batch](http://stackoverflow.com/questions/8177695/how-to-wait-for-a-process-to-terminate-to-execute-another-process-in-batch) – user3469517 Aug 29 '14 at 20:21
  • @user3469517 the sleep command is not in all versions of windows and has been depreciated. – Alex Aug 29 '14 at 20:26
  • possible duplicate of [How to check if a process is running via a batch script](http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script) – npocmaka Aug 29 '14 at 21:19
  • Did your batch script launch program1.exe? – dbenham Aug 29 '14 at 23:07
  • Yes, my batch script launched program1.exe – user3240815 Aug 30 '14 at 00:44

3 Answers3

2

Try like this :

@echo off
Set "MyProcess=Notepad.exe"

:start
tasklist | find /i "%MyProcess%">nul && goto:wait || start %MyProcess%
goto:start

:wait
ping localhost -n 3 >nul
goto:start

Just replace Notepad.exe with the name of your program

SachaDee
  • 9,245
  • 3
  • 23
  • 33
0

you can use a for /f loop with a tasklist to check for the process with a 1 sec timeout using the ping command (you can also use timeout /t 1 >nul instead). The following example is from one of my scripts:

:CMBK
ping -a -n 1 127.0.0.1 1>2>nul
FOR /F "tokens=1,2,3 delims=: " %%A IN ('tasklist /fi "imagename eq program1.exe" /fo list') do set imgnm=%%A
if not '%imgnm%'=='INFO' goto cmbk
Alex
  • 917
  • 5
  • 11
0

Is there an efficient way to check? Maybe a hack that checks this condition once a second?

Use the timeout /t 1 command. If you don't want the "Waiting for 1 seconds, press a key to continue ..." message to appear, use timeout /t 1 > nul.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62