How to supress "Terminate batch job (Y/N)" confirmation?

163

27

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?

Srikanth

Posted 2009-09-04T12:39:12.810

Reputation: 3 769

11"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 – 2015-03-25T09:03:14.877

@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 – 2015-07-20T13:31:38.833

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... – Chev – 2015-09-14T19:43:29.640

This problem is at least causing me to remember and make use of the -n switch for cygwin ping more often. – palswim – 2015-11-25T00:56:21.697

I don't have the reputation to post an answer, but I found the following worked for me on Windows 7 when running an external Java application (although it clears the command-window history for some reason): @CMD /K "java -jar "%~dpn0.jar" %*" & EXIT 0 – Jeff G – 2017-04-04T00:26:39.710

Did you ever try sgmoore's answer? http://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation/35741#35741

– Arjan – 2009-09-27T10:40:44.010

Answers

56

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.

BinaryMisfit

Posted 2009-09-04T12:39:12.810

Reputation: 19 955

1

Here it is: http://superuser.com/a/498798/56101

– Totty.js – 2014-07-08T19:19:33.243

47That's disappointing :( – Srikanth – 2009-09-04T12:42:52.997

Yes it is disappointing. What’s even more disappointing is that the BREAK command, which by default does nothing under XP, could have been used to toggle the prompt… – Synetech – 2009-12-24T19:12:45.907

4

Though it might be true one cannot make the interpreter behave differently, using start like sgmoore suggested at http://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation/35741#35741 seems a perfectly fine workaround to me.

– Arjan – 2010-01-06T16:44:27.817

61

Press Ctrl+C twice.

Kirill V. Lyadvinsky

Posted 2009-09-04T12:39:12.810

Reputation: 5 028

2That's not what the OP asked. He could just as easily type "N" after ^C. – vladr – 2015-05-12T17:35:37.733

3@vladr it's infinitely easier to hold Ctrl and tap C again versus hitting Y then Enter. – Nick T – 2015-12-14T18:27:09.807

23@SebastienDiot You tried out a new technique on a script that had the potential to permanently destroy data? That's... Brave. – Basic – 2016-03-06T04:19:55.340

2@Basic Firstly, it was "my" (personal) database, not the productive system, so only "lost productivity" (download and restore DB again), not "lost data". Secondly, how is "CTRL-C" a "new technique"? :D – Sebastien Diot – 2016-03-07T13:38:31.977

Twice? Not for me: ^C^CTerminate batch job (Y/N)? ^CTerminate batch job (Y/N)? ^C^CTerminate batch job (Y/N)? ^C – Post Self – 2017-10-08T11:19:27.150

@kim366 is there any chance you application was restarted automatically? – Kirill V. Lyadvinsky – 2017-10-09T09:54:21.383

@KirillV.Lyadvinsky I don't know. I was terminating an Express server. I am too new to all of this to say for sure – Post Self – 2017-10-09T15:01:50.023

This solution works only 2-3 times in a row. You can create a loop with some long command inside. You will restart loop only few times. And at some point whole terminal windows is closed. – Sergey – 2018-05-14T16:04:29.190

1@vladr there is a practical difference. If you are running the script from cmd (as opposed to double-clicking the script), Ctrl c + y puts y in command history, which means if you hit up key once, you get y instead of the actual previous command. Ctrl c twice does not have this problem. – cyqsimon – 2020-01-15T07:50:05.307

I tried it in PS 2.0 on Win7 and it worked. However I have seen the repeating prompt too in the same env. It's a mystery. Still, +1. – jcollum – 2011-11-22T19:06:17.077

6This won't work but rather repeats the prompt. – Joey – 2009-09-17T05:25:13.963

Are you sure? I just tried it in an XP command prompt and it worked. I’ll check in DOS 7.11 later, but I’m pretty sure that Enter causes it to repeat the prompt, rather than Ctrl-C. – Synetech – 2009-12-24T19:10:21.637

9

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 http://superuser.com/questions/740004/why-does-ctrl-c-on-terminate-batch-job-prompt-means-carry-on

– Sebastien Diot – 2014-04-10T11:48:23.957

60

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
)

Gringo Suave

Posted 2009-09-04T12:39:12.810

Reputation: 932

perfect, should be accepted! – Totty.js – 2014-07-08T19:18:55.400

I think: start /b /wait cmd /c works better – Sebastian Godelet – 2014-07-15T19:52:07.410

Another problem with this answer is that it won't work if your script wants to read actual input from stdin. – jamesdlin – 2017-07-29T00:11:11.893

2If 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 – 2013-02-22T07:54:27.020

3This works in Windows 7. – William – 2013-02-22T07:54:46.117

17It works, unless you need user input in the batch. – augustomen – 2013-03-27T14:51:41.093

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 – 2013-07-19T23:58:09.280

jpmc's answer worked for me. It does echo the darn Terminate (y/n)? message at then end though, but doesn't stop for it. – ggb667 – 2014-01-24T19:02:12.750

See my answer, if your script relies on %*.

– Olegs Jeremejevs – 2014-02-12T11:36:13.060

31

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.

Frickin' awesome, IMO!

Macke

Posted 2009-09-04T12:39:12.810

Reputation: 963

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: https://github.com/mridgers/clink/releases/tag/0.4.3

– schlamar – 2015-01-14T12:13:28.133

I can confirm that latest Clink now support French prompt "Terminer le programme de commandes" wonderfully... :-) A long supported nuisance at least removed from my computer! Side note: Clink is a perfect match with ConEmu, the first good substitute for Windows' command prompt I have found. – PhiLho – 2015-03-25T09:21:41.030

3

Related: for those using cmder, the setting is on\cmder\config\settings. See the issue related here https://github.com/cmderdev/cmder/issues/1666

– edmundo096 – 2018-02-27T16:54:44.110

damn it @edmundo096 I discovered the setting and was all proudly ready to make my mark on this post, but then I noticed your comment was buried... d'oh. +1, maybe it'll get unburied now :) – Adam Plocher – 2018-11-17T20:35:26.247

It hooks everywhere perfectly! Most usefull answer of the month! Thanks a lot! (ConEmu stoped working, but it's fine, I just sing cmd now) – Vladimir Ishenko – 2018-11-17T22:39:56.553

@edmundo096 You're a hero, thanks! – David Refoua – 2019-03-09T10:55:52.420

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. – Ruslan Stelmachenko – 2019-06-06T19:18:03.010

Clink is fantastic but this doesn't work for me, using cmd.exe or Console2. It still asks and it doesn't get autofilled. – iono – 2013-04-22T04:43:10.567

@tometoftom: Did you change the settings file as above? I had to do that to get it to work. (Note that you have to be an admin to save the file correctly on Windows Vista and up...) – Macke – 2013-04-22T07:53:51.537

Yeah, with the settings changed. I'm an admin :(

I tried doing the equivalent of "Run as Administror" for executables by making a shortcut of the settings file, right-click -> Properties, Shortcut tab, Advanced... then "Run as Administrator" is greyed out. – iono – 2013-04-22T08:25:30.273

Run notepad in admin mode, then edit the file. – Macke – 2013-04-23T12:00:51.393

3Ah, still no dice. deep sigh Microsoft, for God's sake... – iono – 2013-04-23T18:00:20.047

18

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

sgmoore

Posted 2009-09-04T12:39:12.810

Reputation: 5 961

9(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 – 2015-06-25T04:09:19.333

1+1 works for me. However, /b turns off Ctrl+C handling (which is annoying). – Nick Bolton – 2010-04-10T14:58:41.197

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 http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/start.mspx then. Please explain the downvote?

– Arjan – 2009-09-04T18:48:24.730

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 – 2009-09-05T08:54:04.810

All I can say, is that it works for me and the significant point is that the cmd is NOT running a batch file (because it has finished). – sgmoore – 2009-09-05T10:15:38.483

But could one still terminate the application then? So: can Ctrl-C be used to terminate the application that was invoked using start? (Still sounds plausible to me...) – Arjan – 2009-09-07T12:21:12.637

Whether Ctrl-C will terminate the application is independent of whether or not the application is run from a batch file. For example, if (in XP) you start c:\windows\system32\edit, it will not respond to the Ctrl-C regardless of how it is run. However defrag does respond to the ctrl-C. So the simpliest way to test this is to create a batch file that simply says start defrag c: – sgmoore – 2009-09-07T16:00:34.130

4This is fine if you're double-clicking the batch file. But what if you start the batch file in the console? – Helgi – 2012-04-18T18:46:39.857

I'm about to boot an old Windows machine to prove this works, but I already upvoted and me saying it works won't make much difference I guess... So, @vito, did you ever test this answer? This really sounds like the solution that was asked for! – Arjan – 2009-09-27T10:39:57.913

Alright, I finally booted an old Windows machine to confirm this answer. And sgmoore is right, like I already expected. – Arjan – 2010-01-06T16:42:19.867

13

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.

ViperGeek

Posted 2009-09-04T12:39:12.810

Reputation: 368

you should use exit /b to exit the batch script – Sebastian Godelet – 2015-05-07T16:23:20.807

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 – 2018-06-16T22:35:02.123

Why use "cmd /k" (keep the interpreter open) instead of "/c"? Just to be able to see the command's output? – Raúl Salinas-Monteagudo – 2019-03-22T07:15:37.937

+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 – 2012-08-06T15:34:20.440

9

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.

Olegs Jeremejevs

Posted 2009-09-04T12:39:12.810

Reputation: 579

This won't work if myscript.py wants to read from stdin. – jamesdlin – 2017-07-29T00:09:24.983

4

See this Stack Overflow question.

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

Joey

Posted 2009-09-04T12:39:12.810

Reputation: 36 381

2

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.

Charles Roper

Posted 2009-09-04T12:39:12.810

Reputation: 9 646

0

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

user31480

Posted 2009-09-04T12:39:12.810

Reputation:

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.

Ƭᴇcʜιᴇ007

Posted 2009-09-04T12:39:12.810

Reputation: 103 763

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
)

kevinf

Posted 2009-09-04T12:39:12.810

Reputation: 524

0

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

Il Cognato

Posted 2009-09-04T12:39:12.810

Reputation: 1

2I think you may have meant <nul (as the >nul sends all stdout to null). – Brian Phillips – 2011-12-07T13:54:57.453

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!

bonipet

Posted 2009-09-04T12:39:12.810

Reputation:

-2

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

Ricky Boyce

Posted 2009-09-04T12:39:12.810

Reputation: 125

Why the downvote? I have denoted when only using Hyper. Its actually a great solution i use when using windows. – Ricky Boyce – 2018-11-09T22:25:34.607

1The question is about cmd.exe. – Raúl Salinas-Monteagudo – 2019-03-22T07:04:23.673

-3

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 CDM to ask if you want to termininate the batch.

Henri

Posted 2009-09-04T12:39:12.810

Reputation: 1

5Didn't work for me. – Helgi – 2012-04-18T18:47:04.443