问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/14747963/windows-batch-run-a-process-in-background-and-wait-for-it