Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the exit code of an application started with the "cmd" and "start" commands

I have a console application. Interaction with this application is done via TCP/IP.

I also have a test framework for it, which is basically a collection of BATCH scripts (...not my fault). What this test framework does for each test is basically this:

  1. start /min "myapplication.exe" and wait until verification is received that the application is up and running.
  2. send commands via TCP/IP to this application, receive its replies, and check if the timings and values agree with whatever is expected by the specific test.

One problem that I'm currently encountering is that the application exits prematurely due to some internal error. I would like to distinguish between failed tests, and the application crashing. The one and only indication I have for this, is the application's exit code.

So, I tried the following:

start /min cmd /c "myapplication.exe || echo %errorLevel% > exitcode.txt"

and then later on in the test scripts,

if exist exitcode.txt (
    set /p exitcode=<exitcode.txt
    echo ERROR: myapplication.exe returned exitcode %exitcode%.
    goto error
) else (
    goto do_processing
)

but for some strange reason, the text file never appears, even though I sometimes get a dialog about the application crashing, and even though I forcibly make it fail with a known non-zero exit code. The test just goes through do_processing and (of course) results in failure.

EDIT When I run

start /min cmd /c "nonsense || echo %errorLevel% > test.txt"

I sometimes get a text file containing the string 9009, but other times that text file contains the string 0, or sometimes 1, ...What the...?!

EDIT2 If you type

cmd /k "nonsense || echo %errorLevel%"

(note the /k option), you see 0 being printed in the new window, but if you then type echo %errorlevel%, you get 1....

I knew batch was not very sane, but it should at least be consistently insane...

Any ideas on what could be going on here?

like image 853
Rody Oldenhuis Avatar asked Feb 04 '15 10:02

Rody Oldenhuis


People also ask

How do I find my return code using cmd?

To display the exit code for the last command you ran on the command line, use the following command: $ echo $?

What is the command to exit cmd?

You can also use the shortcut key Alt + F4 to close a Command Prompt window.

How do I find the exit code from a previous version of Windows?

Exit Code Of Last Console Command Return True or False depending on whether the last console command or application exited without error or not: # Windows CMD C:\> if %ErrorLevel% equ 0 (echo True) else (echo False) # Windows PowerShell PS C:\> $?

How do I run a program from CMD EXE?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.


1 Answers

As explained here you have to use call instead of start to be able to evaluate the exit codes. call will launch the script in the same variable environment whereas start will run it in a new one which is not accessable from the first script.

like image 61
MichaelS Avatar answered Sep 30 '22 18:09

MichaelS