Home > Software engineering >  Can I call %COMSPEC% without terminating all Windows batch execution?
Can I call %COMSPEC% without terminating all Windows batch execution?

Time:01-31

Came across this oddity of batch behaviour as part of the build process in our systems that I'm trying to automate in Jenkins pipelines.

To skip to the meat of the problem and why I'm encountering it, the batch file I'm calling is a build generated batch file that must be called prior to another executable within the same command window to set up paths and alike for our executable to build our system. When building as a user this is fine, you just open the cmd console, run the batch, then run the executable. However in Jenkins I have to pass all commands effectively as one batch file to make it call within the same window, unless I want to mess around with configuring all those paths by hand using withEnv or something.

Something along the lines of this in my Jenkins groovy script:

// Option 1 - && operator
bat "env_configuration.bat && env_dependent_exec.exe"

// Option 2 - multi line version
bat """env_configuration.bat
env_dependent_exec.exe
"""

// Option 3 - same using calls, yadda yadda
bat """call env_configuration.bat
call env_dependent_exec.exe
"""

// Option 4 - Place these calls in batch file and call that instead
bat "run_it_all.bat"

As part of the batch file however, for whatever reason, it has this command within it.

%COMSPEC%
exit /b 0

This will call "C:\WINDOWS\system32\cmd.exe" and output the Mircrosoft version and related blurb.e.g:

Microsoft Windows [Version 10.0.19045.2486] (c) Microsoft Corporation. All rights reserved.

The catch is, calling this executable will immediately end all batch execution. The line "exit /b 0" isn't actually hit, something whoever created this process I assume never realised. After this batch file process completes, all subsequent commands/calls, (Option 1 above is the easiest to repro) are never hit as all batch processing just stops. This is visible directly in cmd itself, you don't need Jenkins to reproduce.

I will probably wind up just find and replacing this line to comment it out or something... but I refuse to believe I can't find some way of stopping %COMSPEC% from ending all execution of whatever batch file calls it. If I were to guess, I would guess that cmd.exe calls EXIT as it finishes and kills all....

For the sake of interest I have tried modifying the batch file in numerous ways to try and see if I can call %COMSPEC% and still get the rest of the batch script to run afterwards.

:: This fails the same way
call %COMSPEC%
exit /b 0

:: This succeeds to run, but spawns another cmd window that doens't print in the original calling batch file output, so doesn't achieve the original goal
start %COMSPEC%
exit /b 0

:: call %COMSPEC% via subroutine also immediately terminates, we never see "echo 2"
call :comspec_call
exit /b 0

:comspec_call
@echo %COMSPEC% echo 1
%COMSPEC%
@echo %COMSPEC% echo 2

I would guess cmd.exe calls the EXIT command flat on termination, hence the process death, but I've yet to find a means to prevent it doing so...

I've checked %COMSPEC% for flags that might just printout and terminate nicely, but any flags I've found that do provide this information, also terminate with EXIT (I think)

Does anyone has any idea how to call this line and continue execution as I assume the original dev intended? Cheers in advance!

CodePudding user response:

Batch processing doesn’t just stop — you have started another command interpreter, and the one that launched it is waiting for the new instance to terminate. As it doesn’t quit, everything appears to halt and you wind up killing both to get things back to normal.

Create a new batch script that does everything:

:: controller.bat
@echo off
call env_configuration.bat
call some_other.bat
env_dependent_exec.exe
...

Use of the call command causes the command shell to invoke a script using the current interpreter.

Now your Jenkins groovy script should be:

bat controller.bat

(Disclaimer: I don’t know Jenkins, so...)

CodePudding user response:

Thanks to @Duthomhas and @Magoo

The problem here is that a call to %COMSPEC% launches a new command line process that must terminate before the rest of the script can continue. I didn't understand that cmd.exe was spawning a new script that the call script was waiting to terminate.

With a simplified recap:

:: env_configuration.bat
@echo env_configuration before COMSPEC
@%COMSPEC%
@echo env_configuration after COMSPEC

:: controller.bat
@echo controller before env_configuration.bat
@call env_configuration.bat
@echo controller after env_configuration.bat

controller.bat && @echo back in shell

When you run controller.bat the process will output the Microsoft blurb, but halt. If you enter "exit" at this point it will kick out of the terminal launched with %COMSPEC% and then all following script steps continue.

Haven't worked out how to achieve this is a single command line call yet... but at least I now know what is happening! Cheers!

  • Related