4

In a Windows cmd terminal, by cmd, I call command scripts (.cmd), but some of these does an exit [code] without the /B, whereby my Windows terminal is terminated.

How to avoid exit of Windows cmd terminal if called command executes exit without the /B?

2 Answers 2

6

You could invoke scriptname.cmd with cmd /c. That way exit will exit your cmd /c invocation, rather than the ancestral console process.

Test1.bat:

@echo off & setlocal echo Exiting. exit 0 

Test2.bat:

@echo off & setlocal echo Invoking Test1.bat cmd /c Test1.bat echo Still running! 

Output of Test2.bat:

Invoking Test1.bat
Exiting.
Still running!

... and the console window remains open.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the cmd /c handled this nicely, so I will skip using call.
If the Test1.bat sets environment variables that you want to keep, they will not be set unless the script runs inside the current shell. This is what CALL enables. You will lose that functionality. The .bat script should be corrected with EXIT /B.
0

Have you tried to set the exit code to 0? Something like this?

command && exit 0 

I have not much experience with cmd commands but I believe that it should work.

3 Comments

The terminal still terminates with this construction.
Even if you create a bat file with that and invoke it before the exit?
What I need is a call that encapsulates the script, and cmd /c in the answer does the trick.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.