8

I am trying to run a batch file with start /high and still get the return/exit code, i.e. %ERRORLEVEL%. The problem seems to be that command START does not return the exit code that the batch file returns.

We have a simple batch file for testing named BatFileThatReturnsOne.bat.

The contents of BatFileThatReturnsOne.bat are

EXIT /B 1

We are calling this as such:

start /high /wait BatFileThatReturnsOne.bat

But no matter what the batch file returns, the execution of start never has a %ERRORLEVEL% of anything other than 0 (zero).

This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window.

The actual call is:

cmd.exe /C start /high /wait BatFileThatReturnsOne.bat

How do I get start to set the %ERRORLEVEL% to something other than 0 (zero)?

2 Answers 2

8

directly from a cmd window or a batch file you can use

start /high /wait cmd /c BatFileThatReturnsOne.bat

but if you need to start the cmd instance to execute the start command that launchs the batch file then you can use

cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
Sign up to request clarification or add additional context in comments.

1 Comment

Because the cmd is actually being executed from cfn-init in AWS CloudFormation, the 2nd proposed solution worked great. Thanks, I would have never figured that out!
4

Just change EXIT /B 1 by EXIT 1.

As explained in the Table 4 given in this answer about START /WAIT bat command:

When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.

2 Comments

Thanks. The problem was actually a little more complicated than the simple bat file (I just created it to test it). It's actually calling chef-client, which in turn is calling ruby.exe. Neither of those actually have the "EXIT /B 1", so MC ND's solution ended up working well.
I am confused by your comment. My answer solves the problem as stated in the question. How an answer can solve a problem that is not described in the question? (answer: just by chance) :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.