Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress “Terminate batch job (Y/N)” confirmation in PowerShell?

When I press Ctrl+C in PowerShell, I receive:

Terminate batch job (Y/N)?

Similar to https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation, except for Windows PowerShell.

Does PowerShell provide any more control over batch jobs than what CMD does?

like image 955
mikemaccana Avatar asked Aug 22 '16 17:08

mikemaccana


People also ask

How do I disable Terminate batch job yn confirmation?

First, invoke node.exe directly, with a tight loop that requires you to press Ctrl+C to terminate the process. As you'll see, pressing Ctrl+C instantly terminates the process - no confirmation prompt. As you'll see, pressing Ctrl+C now presents the undesired Terminate batch job (Y/N)? prompt.

What is Terminate batch job Y N?

The "Terminate Batch Job" comes from the command prompt (cmd.exe) when you kill (via ctrl-c) an application started from a batch file. cmd.exe wants to know if it should continue processing the rest of the batch file even though the application aborted.

How do I stop a batch file from command line?

When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.

How do I end a batch script?

EXIT /B at the end of the batch file will stop execution of a batch file. use EXIT /B < exitcodes > at the end of the batch file to return custom return codes. Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file, which is the latest error codes from the last command executed.

How to supress “terminate batch job (Y/N) ” Confirmation?

SuperUser - How to supress “Terminate batch job (Y/N)” confirmation? GitHub - Meteor repository - Must press Ctrl+C twice, and get "Terminate batch job (Y/N)?" multiple times Answers varied from hard-core solutions like patching the cmd.exe to bypassing the problem by hitting the Ctrl + C twice.

How do I suppress the terminate batch job prompt in TCC?

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.

Why is there no 'terminate batch job' command in Windows 10?

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.

How to stop a batch job from running multiple times?

GitHub - Meteor repository - Must press Ctrl+C twice, and get "Terminate batch job (Y/N)?" multiple times Answers varied from hard-core solutions like patching the cmd.exe to bypassing the problem by hitting the Ctrl + C twice.


2 Answers

The behavior is neither caused by PowerShell nor can PowerShell change it (as evidenced by the PowerShell source-code repo not containing the prompt message).

The behavior is built into cmd.exe - Powershell, in this case, is calling a .cmd file (batch file), which is interpreted by cmd.exe.

  • If you explicitly control the invocation of the target executable, you can fix this by moving to Powershell - note this has its own considerations, see below.

  • If you do not explicitly control the invocation of the target executable, you're out of luck (unless you're willing to install third-party cmd.exe replacements) and must press Ctrl+C twice in order to terminate execution.

  • A[n ill-advised] workaround is to modify the cmd.exe binary - see article with instructions on how to patch the cmd.exe executable in order to suppress the prompt. Additionally, you can post a feature request on GitHub to request that this behavior be fixed at the source, though that is unlikely to happen for reasons of backward compatbility.

To demonstrate the behavior:

The examples assume that Node.js is installed and that node.exe is therefore in your PATH:

First, invoke node.exe directly, with a tight loop that requires you to press Ctrl+C to terminate the process.

PS> node -e "while (true);"

As you'll see, pressing Ctrl+C instantly terminates the process - no confirmation prompt.

Now, let's create a sample batch file that invokes the same command and invoke that batch file:

PS> "@echo off`nnode -e `"while (true);`"" | Set-Content test.cmd
PS> ./test.cmd

As you'll see, pressing Ctrl+C now presents the undesired Terminate batch job (Y/N)? prompt. (You'd get the same behavior if you ran the batch file from cmd.exe.)

To demonstrate that gulp is a cmd file:

You say you're running your command via gulp's CLI.

On Windows, the entry point for the gulp CLI is gulp.cmd [see update in the bottom section] - i.e., a batch file. That is how it works in general for npm-package "binaries" (executables) implemented as either JS files or shell scripts.

That gulp invokes gulp.cmd can be verified as follows:

# Execute from a project folder that has `gulp` installed as a dependency.
# If `gulp` is installed *globally* 
# Note: CLI `npx` requires npm version 5.2.0+
PS C:\some\NodeJs\project> npx where gulp

You'll see something like:

C:\some\NodeJs\project\node_modules\.bin\gulp
C:\some\NodeJs\project\node_modules\.bin\gulp.cmd

Note that where.exe also lists the extension-less Unix-shell script, ...\gulp; however, from cmd.exe / Powershell such a shell script isn't directly executable, and it is ...\gulp.cmd - the batch file - that is executed.
(If in doubt, place a command such as @set /p dummy="Press a key" at the start of the gulp.cmd file, and you'll see that this command executes when you invoke gulp without the .cmd extension.
Also note that there is no gulp.exe.)

More generally, on Windows, a project's node_modules\.bin subfolder contains pairs of CLI entry points for the CLIs that come with packages that the project depends on locally:

  • node_modules\.bin\<some-cli> is the Unix shell script (whose executing interpreter is controlled via its shebang line).
  • node_modules\.bin\<some-cli>.cmd is the helper batch file for Windows.

Updates and future considerations:

In the context of npm modules, the problem would go away if a PowerShell script (*.ps1) were used as the helper script on Windows. There are tickets for npm, yarn and similar software to do this. There are also some drawbacks:

  • *.ps1 files aren't directly executable from outside of PowerShell, notably from cmd.exe and File Explorer (and changing that is nontrivial).

  • PowerShell still hasn't fully replaced cmd.exe as the default shell, as of Windows 10 (and won't anytime soon, if ever).

When called from PowerShell, a *.ps1 file would be found and run in-process, so a possible solution is for the npm project to also provide *.ps1 helper scripts, which would take precedence over *.cmd files of the same name.

  • Update:
    • Recent versions of npm (verified in 6.14.10) indeed DO install such *.ps1 files.
    • Alternative package manager yarn, since v2 does not seem to use batch files anymore at all, so the original problem is bypassed there; (v1, by contrast, still uses batch files (only); upgrading from v1 must be done on a per-project basis see the migration instructions).
like image 117
mklement0 Avatar answered Oct 14 '22 00:10

mklement0


As the other answer notes, the correct fix is to replace cmd scripts with ps1 versions.

However another workaround for users of the Hyper shell is 'Hyper yes', a plugin that automatically hits y for you when the prompt comes up.

enter image description here

like image 5
mikemaccana Avatar answered Oct 14 '22 02:10

mikemaccana