NaveenBharadwaj
NaveenBharadwaj

Reputation: 1333

Making command prompt wait

@call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err

start cmd /c @call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err

start cmd /c @call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err

start cmd /c @call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err

I have this set of batch files getting executed in order. I want to fork out this so that they execute in parallel. I have done that using start cmd /c. Now this will fork out new command prompt window for each of them. Assume there are some set of statements after this. I want to make sure they get executed only after all the forked batch files are finished. How to achieve this?

Lets say average time taken by each file is: JBOSSbuildWar- 30 minutes WeblogicbuildWar- 35 minutes FEBAPortletWebWar- 30 minutes buildStaticJBoss- 35 minutes

Since the main command prompt window has completed its task in 30 minutes, and the forked batch files needs another 5 minutes to complete, I want the main cmd to wait until the others are done. Kindly help.

Upvotes: 0

Views: 16273

Answers (3)

LoganMurphy
LoganMurphy

Reputation: 89

there's multiple commands, you can choose what one to use,

Pause

Will pause cmd until the user presses any key. Displays message, "Press any key to continue..." I use a certain variation of this command called pause>nul. What it does is pauses the .bat, but doesn't show a message, You just get a blank screen!

TIMEOUT

Multiple syntaxes, very useful command, use this one quite a bit. Sample code: TIMEOUT 10 Can be bypassed by a key press Pauses the execution of command by 10 seconds. You can also structure it like this: TIMEOUT 10 /nobreak Which does not allow the user to press a key to skip it, they must endure it!

Ping

Very nice one. Syntax is like this: PING 1.1.1.1 -n 1 -w 60000 >NUL Probably most complex of the three. PING can be used to delay execution for a number of seconds.

Hope I helped! -Logan

Upvotes: 3

Magoo
Magoo

Reputation: 79982

Easy way:

In your main batch,

del "%temp%\*.processfinished" >nul 2>nul
...
start ....whatever1.bat...
start ....whatever2.bat...
start ....whatever3.bat...
...
wait4all:
for /L %%a in (1,1,3) do if not exist "%temp%\%%a.processfinished" timeout /t 1 >nul &goto wait4all
:: continues here when all (3) processes have finished.
del "%temp%\*.processfinished" >nul 2>nul

Then in each of the (3) subsidiary .bat files, create a file "%temp%\x.processfinished" where x=1 for the first process, 2 for the second and so on.

When the sub-processes have started, the procedure waits until each has created its own ".processcompleted" file by checking whether any of the 3 is missing, if it it, timeout for 1 second and look again. Only if the 3 files are present will it continue.

In all probability, it would be best if the subsidiary processes could take an extra parameter (the name of this sub-process's "processfinished" file) rather than having a fixed number for each.

You could extend this, and use say the date and time to augment the filename so that this entire process could itself be run many times in parallel.

BTW - by starting the procedure with

@echo off

you can remove all of the leading @s (all that does is suppress the command-reporting for that one line.)

Also, start is happier as start "" ....., that is, with an empty window title in quotes as its first argument. This allows other arguments to be "quoted" as necessary - the very first "quoted" argument used is used as the "window title" for the process and is likely to be lost to the sub-process. Routinely assigning a dummy (empty if necessary) "window title" means you don't trip over this problem in the future.

Upvotes: 1

HyFang
HyFang

Reputation: 1

You can add this timer function before each of the commands:

@call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err

Timeout /t 60 /nobreak >nul

start cmd /c @call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err

Timeout /t 60 /nobreak >nul

start cmd /c @call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err

Timeout /t 60 /nobreak >nul

start cmd /c @call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err

Ps: 60 stands for 1 minute, if you want 30 minutes, change it to 1800

Upvotes: 0

Related Questions