Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch: run a process in background and wait for it

I need to start 2 background processes from my batch job and then wait for them. Unix shell analogue is:

myprocess1 -flags1 &
pid1=$!

myprocess2 -flags2 &
pid2=$!

wait ${pid1}
wait ${pid2}

Any ideas?

like image 1000
Maksym Polshcha Avatar asked Nov 21 '25 19:11

Maksym Polshcha


2 Answers

You could solve it, using a start wrapper.

The wrapper starts a process with start /wait and after the process is finished it deletes a file for signaling.

The first process you start through the wrapper, the second you can start with start /wait.
Then you only need to wait for the file.

Echo > waiting.tmp 
Start cmd /c wrapper.bat myprocess1 -flags1
start /wait myprocess2 -flags2

:loop
if exist waiting.tmp goto :loop

content of wrapper.bat

start /wait %*
del waiting.tmp
like image 175
jeb Avatar answered Nov 24 '25 07:11

jeb


Use the START command:

START /WAIT myprocess1 -flags1

The only issue is that AFAIK you cannot let processes run in parallel and wait for both of them -- they have to run sequentially.

You might be able to do concurrency by starting the two processes without START /WAIT, then periodically parse the output of the TASKLIST command to see if they are still running. This is going to be more complicated and of course it's a busy wait, but in theory would allow the processes to run concurrently.

like image 40
Jon Avatar answered Nov 24 '25 08:11

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!