220

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?

9
  • 40
    "I can never remember an instance where I chose not to terminate the batch job." And actually, in most cases, the job is terminated even if you answer No...
    – PhiLho
    Mar 25, 2015 at 9:03
  • @PhiLho - That can't be true, as my scripts always continue after I enter N. I'm curious what scenario you have where the batch script terminates after you enter N.
    – dbenham
    Jul 20, 2015 at 13:31
  • I was hoping this would be easy, but as usual Microsoft has made something simple a pain in the ass. The answers below are all expending way too much effort to avoid this minor annoyance. I'm just annoyed that there's no simple way to resolve this. Seriously Microsoft, what is the point of this prompt? Yes I'm sure, that's why I hit Ctrl + C...
    – CatDadCode
    Sep 14, 2015 at 19:43
  • 3
    I sent Microsoft feedback using feedback hub asking them to add an option to cmd.exe to turn this off: aka.ms/AAaxwcy – if enough people vote for it, maybe just maybe Microsoft might do something about it :) Jan 27, 2021 at 6:38
  • 1
    @a3y3, wow seems like Windows Feeback Hub access control logic is broken, which makes it pretty useless if we can't vote.
    – noseratio
    Jul 2, 2021 at 22:28

17 Answers 17

78

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.

8
83

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
)
11
  • 3
    If you call a batch file from within itself (without using the CALL command), execution is "permanently" transferred. If STDIN is redirected to NUL, any "Terminate Batch Job" prompt will still appear, but won't wait (because STDIN is gone) for input. This works for me ...
    – William
    Feb 22, 2013 at 7:54
  • 4
    This works in Windows 7.
    – William
    Feb 22, 2013 at 7:54
  • 25
    It works, unless you need user input in the batch.
    – augustomen
    Mar 27, 2013 at 14:51
  • 2
    @William, Just calling the script didn't work for me, but I came up with a workaround: @IF ""=="%1" (@%0 CALLED < nul) ELSE (@[your command]). Calls itself recursively, but with an argument the second time. If your script has arguments, you could potentially use the first unused positional argument. That could be problematic if your script has a lot of arguments. My script didn't need arguments or user input.
    – jpmc26
    Jul 19, 2013 at 23:58
  • 2
    You, sir, are a gentleman and a scholar Feb 5, 2021 at 21:05
75

Press Ctrl+C twice.

14
  • 24
    This won't work but rather repeats the prompt.
    – Joey
    Sep 17, 2009 at 5:25
  • 19
    I just killed my database by doing that! The script was waiting on "pause", so I did CTRL-C, and then the prompt came, so I did CTRL-C again, and then the batch file hapilly carried on, and deleted my database! @see superuser.com/questions/740004/… Apr 10, 2014 at 11:48
  • 6
    That's not what the OP asked. He could just as easily type "N" after ^C.
    – vladr
    May 12, 2015 at 17:35
  • 5
    @vladr it's infinitely easier to hold Ctrl and tap C again versus hitting Y then Enter.
    – Nick T
    Dec 14, 2015 at 18:27
  • 45
    @SebastienDiot You tried out a new technique on a script that had the potential to permanently destroy data? That's... Brave.
    – Basic
    Mar 6, 2016 at 4:19
49

Install Clink and change the "terminate_autoanswer" setting. The settings file should be here: C:\Users\<username>\AppData\Local\clink\settings.

# 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. 1

Frickin' awesome, IMO!


1 But you will need to close and reopen cmd.exe before it takes effect.
2 Original unmaintained CLink repo here: http://mridgers.github.io/clink/

15
  • 4
    Ah, still no dice. deep sigh Microsoft, for God's sake...
    – iono
    Apr 23, 2013 at 18:00
  • 3
    @iono do you have non-English Windows version? In this case clink couldn't auto answer the prompt until the 0.4.3 release a few days ago: github.com/mridgers/clink/releases/tag/0.4.3
    – schlamar
    Jan 14, 2015 at 12:13
  • 6
    Related: for those using cmder, the setting is on\cmder\config\settings. See the issue related here github.com/cmderdev/cmder/issues/1666
    – edmundo096
    Feb 27, 2018 at 16:54
  • 3
    For ConEmu users: the settings location is here: c:\Program Files\ConEmu\ConEmu\clink\profile\settings. But you don't need to know it actually. Just run cmd under Administartor (to make config file writable, as it's placed under C:\Program Files where only admins can change files by default) and run clink set terminate_autoanswer 1. This not only sets value for current session, but also writes to config file. You can also see current value if you run this command without last argument. And you can even see the location of config file if you run just clink set. Jun 6, 2019 at 19:18
  • 5
    The author of original clink has not been updating the project for a while. There's an actualized version in which many issues are solved and features added. Get it: chrisant996.github.io/clink Sep 30, 2021 at 9:13
20

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.

For example:

@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.)

10
  • 1
    I don't know if start would indeed help, but it sounds plausible to me. So I wonder if the downvote implies that this would NOT work? Or maybe whoever downvoted does not know Windows' start (or especially start /wait and start /b) command? See microsoft.com/resources/documentation/windows/xp/all/proddocs/… then. Please explain the downvote?
    – Arjan
    Sep 4, 2009 at 18:48
  • start won't work. Whenever cmd is running a batch file, tapping Ctrl+C will cause the currently running process to terminate and show the question.
    – Joey
    Sep 5, 2009 at 8:54
  • 2
    +1 works for me. However, /b turns off Ctrl+C handling (which is annoying). Apr 10, 2010 at 14:58
  • 4
    This is fine if you're double-clicking the batch file. But what if you start the batch file in the console?
    – Helgi
    Apr 18, 2012 at 18:46
  • 11
    (Speaking from Windows 7, not sure if applicable to XP) The unthinking developer hired by Microsoft to code the start command made the dumb decision to make the title of the window both mandatory and optional at the same time - in a rather confusing way. So if ever your %my_command% is enclosed in double quotes, it becomes the title of the window and the first param in %my_params% becomes the command. To be safe, use start "some title here" %my_command% %my_params%. Most just use "" and curse the developer for not using a /TITLE or /T option instead to set the title.
    – ADTC
    Jun 25, 2015 at 4:09
14

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 .BAT file.

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 C:\> prompt:

@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.

4
  • +1 A very novel approach and although I agree ti's not an exact replacement, it works well (although you did worry me when you started your post with "replacing cmd.exe" - security alarms were ringing until I got to the example and realised what you meant)
    – Basic
    Aug 6, 2012 at 15:34
  • you should use exit /b to exit the batch script May 7, 2015 at 16:23
  • 1
    @SebastianGodelet: That's good advice in general, but here the intent is indeed to close the (original) console window, given that the target command is launched in a new window.
    – mklement0
    Jun 16, 2018 at 22:35
  • Why use "cmd /k" (keep the interpreter open) instead of "/c"? Just to be able to see the command's output? Mar 22, 2019 at 7:15
13

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
)

99.(9)% flawless.

2
  • 1
    This won't work if myscript.py wants to read from stdin.
    – jamesdlin
    Jul 29, 2017 at 0:09
  • 1
    What I like about this approach is it can be condensed into one line that you just paste at the top of your original script: IF "%JUSTTERMINATE%" NEQ "OKAY" SET "JUSTTERMINATE=OKAY" & CALL %0 %* <NUL & SET "JUSTTERMINATE=" & GOTO :eof
    – jez
    Jul 16, 2020 at 2:42
8

TCC/LE, which is a free CMD replacement (think of it as CMD++), has an option to suppress the terminate batch job prompt. You can find the option in the TCC Startup Configuration Dialog:

Cancel Batch File on Ctrl-C: Cancel batch file processing without the usual prompt when you press Control-C.

Screenshot:

TCC/LE options

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.

0
5

See this Stack Overflow question.

However, patching cmd.exe is not something I would do for that.

3

Solution using AutoHotkey

  1. Download and install AutoHotkey

  2. Create a script (e.g. terminate.ahk)

  3. Paste the following script

#SingleInstance Force

#IfWinActive ahk_exe cmd.exe
^d::terminate()

#IfWinActive ahk_exe powershell.exe
^d::terminate()

terminate()
{
    SendInput {Ctrl down}c{Ctrl up}
    Sleep 100
    SendInput y{Enter}
}

  1. Execute the script

Now when you press Ctrl+D in powershell.exe or cmd.exe a Ctrl+C y Enter sequence is sent and that terminates the program.

If want to customize the hotkey, see List of Keys

If you want to apply this hotkey to another program, just add this to the script (e.g. Visual Studio Code -> code.exe):

#IfWinActive ahk_exe code.exe
^d::terminate()

The program name can be found at Task Manager > Details

1

Easiest way to suppress the confirmation if you use the Hyper terminal, is to add the hyper-yes plugin.

2
  • 2
    Why the downvote? I have denoted when only using Hyper. Its actually a great solution i use when using windows. Nov 9, 2018 at 22:25
  • 2
    The question is about cmd.exe. Mar 22, 2019 at 7:04
0

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!

0

Simply redirect the batch stdin to null by adding < nul to the end of the command.

1
  • 2
    I think you may have meant <nul (as the >nul sends all stdout to null). Dec 7, 2011 at 13:54
0

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.

Using 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 .\host.bat.

0

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
)
0

I don't know your context. It is really helpful if you can tell how you launch the application (by start or directly the path). If you can translate the batch script into one PowerShell script, then pwsh -f script_file may help you.

I read this QA when I tried use msys2 in Windows terminal. In Windows terminal, we can use pwsh -c "$env:MSYSTEM='UCRT64';$env:CHERE_INVOKING='enabled_from_arguments';D:\msys64\usr\bin\bash -l" to function as D:\msys64\msys2_shell.cmd -defterm -here -no-start -ucrt64 -shell bash (This translation needs a bit reading of the batch script). So we don't need the batch script then.


Notice we don't use -NoExit for pwsh which is one default parameter for "Developer PowerShell for VS 2022" in Windows terminal, so I guessed maybe also one similar parameter for start which is used by msys2_shell.cmd when not using -no-start. And by reading its doc in ss64, we can use /B option which is also said by Arjan's commment and Nick Bolton's. So start /B bash -l !SHELL_ARGS! will also work.

3
  • Avoid posting answers to old questions (more than 14 years!) that already got well received answers unless you have something substantial and new to add.
    – Toto
    2 days ago
  • @Toto Sorry, temporarily I can't post one comment here. When I can post one comment, I will make one summary of my answer which uses pwsh -c/-f to function similar to start /B at sgmoore's answer. Is that fine?
    – An5Drama
    2 days ago
  • Posting good answers to questions that need them is the best and fastest way to earn the rep you need to perform more actions on this site. 2 days ago
-4

At the end of your script, just add the following command:

echo

This will not "damage" the behavior of your script, and it seems to prevent CMD to ask if you want to termininate the batch.

1
  • 5
    Didn't work for me.
    – Helgi
    Apr 18, 2012 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .