Page 1 of 1

execute multiple commands at the same time?

Posted: 13 Aug 2012 13:02
by ©opy[it]®ight
Hello everyone,

I'm working on a script that does a couple of tasks, but i would like to run those commands simultaneously.

What i've tried so far is the following:

Code: Select all

START "" %TASK1%
START "" %TASK2%
START "" %TASK3%


Now, this seems to work and all but i also found that you can achieve the same thing using the following code:

Code: Select all

CMD /C %TASK1%
CMD /C %TASK2%
CMD /C %TASK3%


And i've even found examples where the following combination was used:

Code: Select all

START "" CMD /C <some command>


Now my question: Which of the above methods is prefered?
I would like to hear it from you ;)

/©opy[it]®ight

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 15:14
by Dos_Probie
It really depends on how you are running your tasks or applications..
example: If running applications from RunOnceEx then you would need
to use the cmd /c .. but if doing tasks on post installs then the Start ""
would be fine .http://gosh.msfn.org/using_runonceex.htm
Of course you want to test and retest to ensure that it works the way
you want it..

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 15:27
by aGerman
If "at the same time" means that you want them to run asynchronously then you have to use the START command. Whether or not you should run it in another cmd process depends on what exactly you want to execute. In case of separate batch files use
START "" CMD /C ...

Regards
aGerman

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 15:56
by ©opy[it]®ight
Thanks for the answers :-)

So (if i got it right), there's a difference between running programs
using START "" , using CMD /C and using START "" CMD /C?

I initially thought that START executes a command interpreter whereas CMD /C is one, on itself.
I'm basically trying to find out in what way the above variations differ from each other.

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 16:38
by aGerman
START runs asynchronously (the batch file executes the next line immediately).
CMD /C runs synchronously (the batch file does wait until the called command was finished).
The reason why I recommend you to combine both in case of executing other batch files is that these windows don't close normally otherwise.

Regards
aGerman

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 16:43
by Liviu
©opy[it]®ight wrote:So (if i got it right), there's a difference between running programs using START "" , using CMD /C and using START "" CMD /C?

I initially thought that START executes a command interpreter whereas CMD /C is one, on itself.

First difference is that START can also launch non-executable files by association, for example "start http://www.google.com".

This is an attempt to summarize some of the other differences, note that some depend on whether the target is a console program vs. a GUI.

Code: Select all

                         runs inside     new instance        runs
                       parent console   of cmd.exe (*)   asynchronously

do-pause.cmd                yes              no               no
cmd /c do-pause.cmd         yes             yes               no
start /wait do-pause.cmd     no             yes               no
start do-pause.cmd [1]       no             yes              yes

notepad.exe [2]             n/a              no              yes
start /wait notepad.exe     n/a              no               no


(*) when running inside a new instance of cmd.exe
 -  changes to environment variables are not effected back in the original environment
 -  calling "exit" in a batch closes the new instance, not the original one

[1] same as "start cmd /k do-pause.cmd"
[2] same as "start notepad.exe"
You can test the above by creating a simple batch file do-pause.cmd containing just:

Code: Select all

@pause
@exit

Liviu

Re: execute multiple commands at the same time?

Posted: 13 Aug 2012 18:02
by ©opy[it]®ight
Oh wauw. aGerman and Liviu just nailed it.

Thank you so much for the invaluable feedback 8)

batch closed :mrgreen: