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

回眸只為那壹抹淺笑 提交于 2019-12-24 01:54:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!