Search code examples
windowsbatch-filewindows-shell

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?


Solution

  • 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