6

I want to run one batch file, that start the other batch files. I looked at a similar question posted here: How to run multiple .BAT files within a .BAT file

I followed the example (specifically the very last suggestion) and it worked...partially. It did launch the batch files that I needed to. However, in order for the applications to function properly, some of these batch files have to open, and then run their course for a few seconds, before the next the next batch file launches, otherwise they won't be registered. Specifically, the first batch file launches a web applications server (JBOSS 5.1), then the next batch file opens a pool manager, then the other two launch distribution servers. When I run my batch file that calls the others, they all launch nearly simultaneously, and they do not register each other. Can I even do this with a batch file? Or do I have to go into the code of the other batch files and make changes there? I want to avoid that at all costs.

Here is what I have so far:

start cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat

start cmd /k CALL batch1.bat

start cmd /k CALL batch2.bat

start cmd /k CALL batch3.bat
Community
  • 1
  • 1
David Horvath
  • 61
  • 1
  • 1
  • 3
  • You can refer to these answers : http://stackoverflow.com/questions/3023543/execute-batch-file-after-another-batch-file-completes – Finickyflame Dec 21 '12 at 17:31

4 Answers4

6

You can drop the start cmd /k and just use CALL.

CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat
CALL batch1.bat
CALL batch2.bat
CALL batch3.bat
aphoria
  • 19,796
  • 7
  • 64
  • 73
  • What purpose does the start cmd /k have then? – David Horvath Dec 21 '12 at 19:55
  • 2
    The `START CMD /K` starts a new `CMD` process, runs the batch file and then exits. At the very least it's unnecessary, and could cause you problems if your batch files interact, i.e. `batch1.bat` sets variables that `batch2.bat` uses, etc. – aphoria Dec 21 '12 at 22:46
4

Answer:

Add the /wait option to the start command.

WAIT        Start application and wait for it to terminate.

Example:

start /wait cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat

start /wait cmd /k CALL batch1.bat

start /wait cmd /k CALL batch2.bat

start /wait cmd /k CALL batch3.bat

Otherwise just use a ping delay between the starts. (See user706837's Answer)

References:

Technet, Rob, SS64, DosTips

David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • I apologize for coming across as naive, but keep in mind that I am VERY new to programming. I am most concerned about the last two commands, and I tried using "WAIT" but I am not sure if I am doing it correctly. It wasn't working how I tried it:line 1: start cmd /k CALL batch2.bat line 2: WAIT start cmd /k batch3.bat. Do I have to use the backslash? And where do I put the "WAIT". In the beginning of the line? After start? – David Horvath Dec 21 '12 at 18:14
  • @DavidHorvath I have updated my answer to show some examples. Also welcome to learning Batch. It's a lot of fun and headaches. First rule when learning batch is to use the help `/?` option often. So to understand each of the commands you are using type in on a command prompt `start /?`, `call /?`, and `cmd /?`. or visit the microsoft technet pages. http://technet.microsoft.com/en-us/library/cc772390(v=ws.10).aspx – David Ruhmann Dec 21 '12 at 18:37
  • Thank you so much. I tried the ping delay, and it's working. Obviously it's a bit crude since I do not know the exact time, so I am just playing around with different delays till I get the best sequence. – David Horvath Dec 21 '12 at 18:56
  • I also just tried start /wait...and it worked much better. Thanks again for all of your help. I also bookmarked that microsoft technet page. I have a feeling that I will be referencing it extensively. – David Horvath Dec 21 '12 at 18:58
1

Whenever I have batch files that depend on another I either: 1. nest them; meaning, if batch1 needs to run before batch2, then I add batch2 within batch1. 2. put a 'sleep' call within batch2. This is only possible if you are fairly certain of the startup duration for batch1.

A sample sleep command is:

ping 127.0.0.1 -n 4 > null

This will make the batch file wait for 3 seconds. (Because there are only 3, 1 second sleeps, between each of the 4 echos)

Examples:

start cmd /k CALL D:\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\bin\run.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch1.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch2.bat
ping 127.0.0.1 -n 4 > null
start cmd /k CALL batch3.bat
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
Jose Leon
  • 1,615
  • 3
  • 22
  • 30
  • These batch files I am using came with the software I have installed, so if nesting involves editing those batch files, I would be hesitant to do it in case I mess something up. I guess I could create a backup file. I am confused about the "ping 127.0.0.1 -n 4 > null" line. I recognize the 127.0.0.1 as the loopback address, but do I put this inbetween my start commands? As in line 1: start cmd /k CALL batch2.bat line 2: ping 127.0.0.1 -n 4 > null line 3: start cmd /k CALL batch2.bat. – David Horvath Dec 21 '12 at 18:20
  • Thanks, I have tried this and it's working great. I appreciate the help. – David Horvath Dec 21 '12 at 19:03
1
There are multiple ways for that.

1.

rem echo call A

CALL a.bat

rem echo call B

CALL b.bat

rem echo call C

CALL c.bat

rem pause

--------------------

2.

echo call A

start cmd /k CALL a.bat


echo call B

start cmd /k CALL b.bat

echo call C

start cmd /k CALL c.bat

pause

---------------------

Here the difference is-

start cmd /k

   It creates these many instances. So we can see multiple number of CMD prompts.


CALL

   Each descendent CALL waits for the completion of the previous CALL.
Chang
  • 435
  • 1
  • 8
  • 17