In cmd, when we press Ctrl+C we get the target application terminated but if the target application is called from a batch file, we get this "Terminate batch job (Y/N)" confirmation. I can never remember an instance where I chose not to terminate the batch job. How can we skip this confirmation?
AFAIK you can't as this behavior is by design and controlled by the command interpreter. There is no method of "mapping" or even "intercepting" this unless you de-compile and recompile the interpreter directly.
Press Ctrl+C twice.
At this site, I found an effective solution:
script2.cmd < nul
To not have to type this out every time I made a second script called
script.cmd in the same folder with the line above. I've tested this technique on XP only, but others have confirmed it on Win 7.
Nathan adds: another option is to put the following code at the top of script.cmd which does the same thing in one file:
rem Bypass "Terminate Batch Job" prompt. if "%~1"=="-FIXED_CTRL_C" ( REM Remove the -FIXED_CTRL_C parameter SHIFT ) ELSE ( REM Run the batch with <NUL and -FIXED_CTRL_C CALL <NUL %0 -FIXED_CTRL_C %* GOTO :EOF )
# name: Auto-answer terminate prompt # type: enum # Automatically answers cmd.exe's 'Terminate batch job (Y/N)?' prompts. 0 = # disabled, 1 = answer 'Y', 2 = answer 'N'. terminate_autoanswer = 1
This then "just works" with any cmd.exe window. You don't need to alter what's running or otherwise, since clink piggy-backs on cmd.exe.
Frickin' awesome, IMO!
If you don't need to do anything in the batch file after your application finishes normally, then using the
start command ensures that the batch file is already finished by the time you press Ctrl-C. And hence the message will not appear.
@echo off set my_command=ping.exe set my_params=-t www.google.com echo Command to be executed by 'start': %my_command% %my_params% :: When NOT using /B or /WAIT then this will create a new window, while :: execution of this very batch file will continue in the current window: start %my_command% %my_params% echo. echo This line will be executed BEFORE 'start' is even finished. So, this echo batch file will complete BEFORE one presses Ctrl-C in the other window. echo. :: Just for testing use 'pause' to show "Press any key to continue", to see :: the output of the 'echo' commands. Be sure to press Ctrl-C in the window :: that runs the 'ping' command (not in this very window). Or simply remove :: the next line when confused: pause
(Tested on Windows XP.)
I've been fighting with this desire to avoid the "Terminate batch job" prompt for a little while.
My latest epiphany is a bit of a sleight of hand (or console window), by replacing one instance of
cmd.exe with another. This is accomplished by executing the command/program via
start cmd /k followed immediately by
exit in the
The original console window disappears and the replacement one can be stopped cleanly via Ctrl-C.
Consider the following example of a
traceroute that can be interrupted by Ctrl+C, or allowed to complete, returning the user to the
@echo off set timeout=100 if not "%2"=="" set timeout=%2 start cmd /k tracert -w %timeout% %1 exit
The environment substitution of a new command interpreter may not be for everyone, but, to the naked eye, looks and works well for me.
Gringo's solution is good, but doesn't work well with scripts which pass along the argument list (i.e.
python myscript.py %*), since
SHIFT doesn't update
%*. There are workarounds, but they have certain limitations.
Here's the modification I ended up with:
IF [%JUSTTERMINATE%] == [OKAY] ( SET JUSTTERMINATE= python myscript.py %* ) ELSE ( SET JUSTTERMINATE=OKAY CALL %0 %* <NUL )
However, patching cmd.exe is not something I would do for that.
In my case it was the ping.bat file that was right in my user directory (C:\Users\ in Vista or C:\Documents and Settings\ in XP) that was holding the batch job indeterminately.
This batch file was executed whenever I ran ping from the command prompt where the current directory is my user directory. Ping-ing from the Run window, or from other user's directory was running well.
Removed the file from my user dir and the problem was resolved!
Start works, but now the window opened by the batch file is changed from the options I had and the "properties" are disabled (won't respond).
Simply redirect the batch stdin to null by adding < nul to the end of the command.
I ran into this with an EXE that seemed to throw ^C to the parent batch on exit, causing the "Terminate Batch Job" prompt even on a clean exit.
The solution I chose to use was running the batch with "Start", similar to other answers, but from a PowerShell prompt (or via the PowerShell interpreter method from CMD, if you choose).
It's 2018 now, and in Windows 10 Microsoft has begun to supplant CMD with PowerShell as the preferred command prompt, so it's readily available in the GUI, by default.
Start is an alias for Start-Process.
When run, it just launches and returns. So when you stop the launched process, there is no "Terminate Batch Job" prompt.
By default it doesn't wait, so no additional arguments beyond the command and it's arguments are required.
start mything.exe -mythings -arguments in my batch worked perfectly.
In PowerShell scripts must be preceded by their path to be launched, so I run my batch file as
Cancel Batch File on Ctrl-C: Cancel batch file processing without the usual prompt when you press Control-C.
If you've never heard of TCC/LE before, here's some blurb from the site:
TCC/LE is a replacement for the CMD command line (the default Windows command prompt). TCC/LE is a superset of CMD, with 111 internal commands (CMD has fewer than 40), 240 internal variables and functions, and hundreds of enhancements to existing CMD commands.
TCC/LE works with your existing command line applications and batch files, but offers major improvements in command line and batch file capabilities, and adds thousands of new features to your command prompt windows.
I used TCC/LE for years and 4NT before it. I've got a lot of love for it and can recommend it. Only reason I don't still use it is because I almost exclusively use PowerShell now.
A prime reason to suppress the "Terminate batch job (Y/N)", is to run a program in a loop (eg: re-run in case it crashes).
This https://stackoverflow.com/a/8185270/1170023 helped lead to this solution:
@ECHO OFF rem assumes you have installed pslist from sysinternals to windows PATH :RUN echo Starting GoldenEye Dedicated Server start "" srcds.exe -console -game gesource +maxplayers 16 +map ge_complex :LOOP pslist srcds >nul 2>&1 if ERRORLEVEL 1 ( goto RUN ) else ( rem echo Dedicated server is still running timeout /t 5 >nul goto LOOP )
At the end of your script, just add the following command:
This will not "damage" the behavior of your script, and it seems to prevent CDM to ask if you want to termininate the batch.