Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exit /B 0 does not work

Tags:

batch-file

cmd

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!

like image 509
murxx Avatar asked May 21 '10 11:05

murxx


2 Answers

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:

The DOS online help (HELP EXIT) doesn't make it clear that the /B parameter exits the current instance of script which is not necessarily the same as exiting the current script. I.e. if the script is in a CALLed piece of code, the EXIT /B exits the CALL, not the script.

So you definitely don't want exit /b 0 in this case. If just exit 0 doesn't work, try GOTO:EOF.

like image 124
Vicky Avatar answered Nov 29 '22 17:11

Vicky


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

@echo off
start "" /b /wait cmd /c "startServer.bat"
if ERRORLEVEL 1 echo Exit code is one  & exit /b 1
if ERRORLEVEL 0 echo Exit code is zero & exit /b 0

Child batch file

@echo off
exit /b 0

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):

start "" /wait /b cmd /c "startServer.bat"

But this does work exactly as expected:

start "" /b /wait cmd /c "startServer.bat"

The only difference is swapping the /b and /wait switches.


I discovered this by accident, using the following steps:

  • Checked all the documentation I could find on start and call and cmd
  • Banged my head on the wall for a few hours trying everything I could think of
  • Gave up, and came back 24 hours later

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!

like image 27
JonathanDavidArndt Avatar answered Nov 29 '22 17:11

JonathanDavidArndt