1

I have a script like this:

@start /b cmd /c net stop "Service"
@start /b cmd /c net stop "Service"
@start /b cmd /c net stop "Service"
@start /b cmd /c net stop "Service"
@start /b cmd /c net stop "Service"
pause

However, the pause command gets executed before all of the lines above it in the script have been executed. I need to use @start /b cmd /c so that it doesn't have to wait for the "Stopping Service..." after each service (about 100) of time. Is there any way to tell cmd to wait until all of the @start /b cmd /c net stop "Service" commands have completed then run the pause?

To see what I'm talking about run this batch file:

@start /b cmd /c echo Hello
@start /b cmd /c echo Hello
@start /b cmd /c echo Hello
@start /b cmd /c echo Hello
@start /b cmd /c echo Hello
@start /b cmd /c echo Hello
pause

Instead of the Pause occurring after all of the Hello have been printed, it happens in the middle:

enter image description here

10
  • Can't answer at the moment since this is on hold - I ran the batch and the pause does get thrown in there before the last echo. My workaround is to just add a timeout 1 /nobreak >nul before your pause. This may need tuning depending on what services your stopping or if they can cause issues, but at least for your example the invisible one second delay seemed to do the trick.
    – mael'
    May 25, 2019 at 17:44
  • It still doesnt work the timeout still goes in between the @start /b cmd /c echo Hello I dont want to add a timeout because sometimes it takes long to end the services and sometimes it ends them quick.
    – PolisP2
    May 25, 2019 at 17:58
  • is it pausing between each line for you? when I did the echo test you suggested it only paused once right before the final hello.
    – mael'
    May 25, 2019 at 18:25
  • for me the pause was at the end but when Im running time consuming tasks such as the net stop, the pause command goes in between the net stops, for example The service has stopped successfully Press any key to continue The service has stopped successfully The service has stopped successfully The service has stopped successfully Like that even though the pause is at the end. Sorry it looks unclear i couldnt press enter like i could in the main question.
    – PolisP2
    May 25, 2019 at 18:46
  • Welcome. I took considerable liberty in an attempt to consolidate your question in order to make the problem you're facing more clear. Please review it and make any adjustments as needed. In particular I couldn't tell how to handle the sentence that says, "I need to use @start /b cmd /c for sake of time." This is a very interesting question and I look forward to you're getting an answer. May 25, 2019 at 21:12

1 Answer 1

1

The command start starts an aplication in it's own context, appart from what you current console is doing, that's why when it's done it will return the output, once your current terminal is done with whatever is doing. That's why even though you send all this start command, your pause is not waiting for all of them to finish It's just waiting long enough to start the processes, That's why your pause appears in the midle of them it's just a thing of timing.

So to solve you problem you can wait for them to finish with /wait or making a call to a second batch script, this way you can wait for some commands and not others, if you preffer:

Wait technique for your script

@start /wait /b cmd /c echo Hello
@start /wait /b cmd /c echo Hello
@start /wait /b cmd /c echo Hello
@start /wait /b cmd /c echo Hello
@start /wait /b cmd /c echo Hello
@start /wait /b cmd /c echo Hello
pause

Technique with laucher script

@echo off
title Laucher

:: This scripts don't need to be waited
start /b cmd /c 'script1.bat'
:: This script needs to be waited
start /wait /b cmd /c 'script2.bat'

Script1

@echo off
title Script to run without waiting

SomeProcessThatDoesn'tNeedsToBeWaited

Script2

@echo off
title Script to run to be waited

SomeProcessThatNeedsToBeWaited

cheers

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .