Wait until several processes have ended

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SmurfCowboyJoe
Posts: 3
Joined: 30 May 2012 06:41

Wait until several processes have ended

#1 Post by SmurfCowboyJoe » 30 May 2012 06:47

Hallo,

in my batch script I start some processes with 'start xyz abc' in a for loop. So, they are all running concurrently. What I want to do is that my batch script does not continue until these processes have been ended. Is there any way in batch to achieve this?

Thx a lot
SmurfCowboyJoe

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Wait until several processes have ended

#2 Post by foxidrive » 30 May 2012 07:34

use the /wait switch.

start "" /w "c:\program files\folder\file.exe"


This will show you the switches:
start /?


If you start several processes at once then you cannot use the /wait switch, or you can run them in sequence.

SmurfCowboyJoe
Posts: 3
Joined: 30 May 2012 06:41

Re: Wait until several processes have ended

#3 Post by SmurfCowboyJoe » 30 May 2012 07:44

Yes, I need to start them at once. This is the problem. Is there a way to use takslist or something to check whether a the process is still running?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Wait until several processes have ended

#4 Post by foxidrive » 30 May 2012 08:01

SmurfCowboyJoe wrote:Yes, I need to start them at once. This is the problem. Is there a way to use takslist or something to check whether a the process is still running?


This sort of code should work:



within your loop

start "" "exename1.exe"
start "" "exename2.exe"
start "" "exename3.exe"
start "" "exename4.exe"
call :delay
your further commands here
)


goto :EOF
:delay
set "var="
tasklist /FI "imagename eq exename1.exe">nul &&set var=1
tasklist /FI "imagename eq exename2.exe">nul &&set var=1
tasklist /FI "imagename eq exename3.exe">nul &&set var=1
tasklist /FI "imagename eq exename4.exe">nul &&set var=1
if defined var ping -n 60 localhost>nul & goto :delay

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Wait until several processes have ended

#5 Post by Squashman » 30 May 2012 08:05

I was looking at using TASKLIST and piping it to the FINDSTR command and then doing conditional execution based on that output with the double ampersand but I am getting some really weird results on my machine with that code.

I have notepad and another program called DeskPins running on my system right now. And for some reason it will not always find them or it will only find one of them. Even when I just try to find one of them it seems to not even find it. I have never seen this happen before so I don't know what is going on with my machine right now.

Code: Select all

H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"
notepad.exe                 5864 Console                 0      3,912 K

H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"
                                                                               notepad.exe                 5864 Console                 0      3,912
H:\>tasklist /NH |findstr /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe"
notepad.exe                 5864 Console                 0      3,912 K

H:\>tasklist /NH |findstr /C:"notepad.exe"
notepad.exe                 5864 Console                 0      3,912 K

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"
notepad.exe                 5864 Console                 0      3,912 K

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe"
DeskPins.exe                2908 Console                 0        420 K

H:\>tasklist /NH |findstr /C:"DeskPins.exe" /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe" /C:"notepad.exe"
DeskPins.exe                2908 Console                 0        420 K

H:\>tasklist /NH |findstr /C:"DeskPins.exe" /C:"notepad.exe"

H:\>tasklist /NH |findstr /C:"DeskPins.exe" /C:"notepad.exe"
notepad.exe                 5864 Console                 0      3,912 K

H:\>


Ultimately I thought you would be able to do something like this.

Code: Select all

start "" "notepad.exe"
start "" "DeskPins.exe"
:Loop
tasklist /NH |findstr /C:"notepad.exe" /C:"DeskPins.exe" &&GOTO :Loop

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Wait until several processes have ended

#6 Post by foxidrive » 30 May 2012 08:11

I read here in Dostips that when using the /c: switch more than once that you have to also use the /i case insensitive switch, because of a bug.


EDIT:

Yes, your technique works well too squashman.

Code: Select all

@echo off
start "" "notepad.exe"
start "" "calc.exe"
:Loop
tasklist /NH |findstr /i /C:"notepad.exe" /C:"calc.exe" >nul && (
ping -n 60 localhost>nul
GOTO :Loop
)
pause

SmurfCowboyJoe
Posts: 3
Joined: 30 May 2012 06:41

Re: Wait until several processes have ended

#7 Post by SmurfCowboyJoe » 30 May 2012 08:29

Thx a lot. But there is a another little problem. The process I start, is a java application. Therefore, in the tasklist there are several java.exe entries show but I dont know which of them has been started by me. I need a way to distinguish started process in my batch script and processes that have been executed from somewhere else.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Wait until several processes have ended

#8 Post by Fawers » 30 May 2012 09:42

SmurfCowboyJoe wrote:Thx a lot. But there is a another little problem. The process I start, is a java application. Therefore, in the tasklist there are several java.exe entries show but I dont know which of them has been started by me. I need a way to distinguish started process in my batch script and processes that have been executed from somewhere else.

It's probably the last one. TASKLIST lists processes in order of execution. I mean, at least on my machine it does.

Try it. Run notepad.exe, or cmd.exe, or the Java application itself, and then run tasklist.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Wait until several processes have ended

#9 Post by Squashman » 30 May 2012 09:59

SmurfCowboyJoe wrote:Thx a lot. But there is a another little problem. The process I start, is a java application. Therefore, in the tasklist there are several java.exe entries show but I dont know which of them has been started by me. I need a way to distinguish started process in my batch script and processes that have been executed from somewhere else.

Try and be more specific on your requirements from now on.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Wait until several processes have ended

#10 Post by Squashman » 30 May 2012 10:00

foxidrive wrote:I read here in Dostips that when using the /c: switch more than once that you have to also use the /i case insensitive switch, because of a bug.


EDIT:

Yes, your technique works well too squashman.

Code: Select all

@echo off
start "" "notepad.exe"
start "" "calc.exe"
:Loop
tasklist /NH |findstr /i /C:"notepad.exe" /C:"calc.exe" >nul && (
ping -n 60 localhost>nul
GOTO :Loop
)
pause

Good Idea on putting the delay in there.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Wait until several processes have ended

#11 Post by Squashman » 30 May 2012 10:04

Code: Select all

tasklist /NH /FI "username eq Squashman"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Wait until several processes have ended

#12 Post by Squashman » 30 May 2012 10:12

Another option would be to use the /V switch with the TASKLIST command and then pipe it to Findstr and have Findstr look for the Specific Window Titles.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Wait until several processes have ended

#13 Post by Aacini » 30 May 2012 15:20

SmurfCowboyJoe wrote:in my batch script I start some processes with 'start xyz abc' in a for loop.
You don't said the way you use the for loop to start the processes, so I assume there are several differently named data files that are started with the same application.

You may use a small Batch file instead to directly start the application; this way, the Batch file: 1- Create a file with unique name that is used as a flag, 2- Start the application, and 3- Delete the flag file. For example, instead of:

Code: Select all

for %%f in (*.xyz) do start "%%f" application %%f abc
you may use:

Code: Select all

for %%f in (*.xyz) do start "%%f" starter.bat %%f abc
so Starter.bat is:

Code: Select all

@echo off
echo Anything > %1.flg
application %1 %2
del %1.flg

This way, to wait for all concurrent processes to end:

Code: Select all

@echo off
for %%f in (*.xyz) do start "%%f" starter.bat %%f abc
:wait
ping -n 60 localhost>nul
if exist *.flg goto wait
pause

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Wait until several processes have ended

#14 Post by dbenham » 31 May 2012 05:43

@Aacini - I was thinking of posting the same solution, but you beat me to it. :D

One slight potential problem:

It takes time for the STARTed processes to initialize. I believe your master script could potentially begin checking for .flg files before the STARTed processes have finished initializing, so it could exit prematurely.

This could easily be solved by having each starter.bat write two flags: xxx.started and xxx.running

Then you would have two conditions to check to see if all of the processes have completed.

1) count the number of xxx.started files and confirm it matches the number of started processes
2) verify there are no xxx.running files

Once both conditions are true, the master script can delete all of the xxx.started files and exit.


Dave Benham

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Wait until several processes have ended

#15 Post by Aacini » 31 May 2012 11:32

@dbenham: Another possible, easier solution is to create the .flg files in the master script and let the starter.bat file just delete they:

Code: Select all

@echo off
for %%f in (*.xyz) do (
   echo Anything > %%f.flg
   start "%%f" starter.bat %%f abc
)
:wait
ping -n 60 localhost>nul
if exist *.flg goto wait
pause


Antonio

Post Reply