I have the following problem:
I have created a batch script which calls itself in there (for being able to write a log in parallel). In the script I start another process (like start startServer.bat
) which starts up a java process and keeps opened up all the time.
In my original script I wait 30 seconds, check if the process is running and do an:
exit /B 0
Unfortunately that does not work, the window shows that the exit /B 0 is being evaluated, but the window still keeps open. When I close the window with the other process (meaning the "child" processes started up in my .bat) my script continues its run.
So:
scriptA.bat
-> in there I call: start startServer.bat
-> wait 30 seconds
-> check is server is started
-> exit /B 0
Process hangs up!
What's very odd, if I wrap another script around, like:
scriptB.bat
-> call scriptA.bat
-----> in there I call: start startServer.bat
-----> wait 30 seconds
-----> check if server is started
-----> exit /B 0
-> scriptA.bat continues without any hangup!
I also tried the same with exit 0 (without /B) also, same result! In the first case it hangs up, in the second case my window closes as expected...
Has anyone of you ever had such a problem before and knows what's wrong here? Process hangs up!
I guess your problem lies within the
start
command. The following excerpt from thestart /?
help might point to the issue:As a solution you could try to modify the start command like this:
There's a good explanation of all the options for exiting a batch script here: http://www.robvanderwoude.com/exit.php
Specifically, from that page:
So you definitely don't want
exit /b 0
in this case. If justexit 0
doesn't work, tryGOTO:EOF
.The earlier answer from Vicky is very good. There is some additional undocumented (or, at least, unclear) behaviour going on here.
In your question, you have a somewhat more complicated situation, but let's say you are calling/starting a batch file from the original, using
exit /b 0
in the called batch file, and expecting that the ERRORLEVEL is accessible in the original.Original
Child batch file
To get this to work, the
start
command must be used with the certain options. Depending on the options, they may need to be in a specific order. (!)According to the docs at SS64 on Start, you should be able to use the
/b
and/wait
switches. The documentation does not state that the order of these switches matters, but it does.For instance, this will NOT work (commands run out of order, and ERRORLEVEL is not returned):
But this does work exactly as expected:
The only difference is swapping the
/b
and/wait
switches.I discovered this by accident, using the following steps:
start
andcall
andcmd
I did not try anything new, I just started over, and it worked the first time. Comparing to previous file versions showed me this (apparently) small difference. Turns out, there is no such thing as a "small" change!