开发者

exit /B 0 does not work

I have the following problem:

I 开发者_如何学Gohave 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!


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.


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!


I guess your problem lies within the start command. The following excerpt from the start /? help might point to the issue:

command/program

If it is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.

If it is not an internal cmd command or batch file then it is a program and will run as either a windowed application or a console application.

As a solution you could try to modify the start command like this:

start "" cmd /c "startServer.bat"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜