包详细信息

ctrlc-windows

thefrontside271.9kMIT2.2.0

Send CTRL-C to a process on Windows

自述文件

ctrlc-windows

Send CTRL-C to a Windows console application

Long running stateful processes like servers often hold a lot of system resources that they need to operate. These are things like communication ports, file descriptors, database connections, etc... At some point, when it is time for that process to shut it needs to release all those resources back to the operating system and exit. This process of freeing up all the things that were being used is called "graceful shutdown" and is really important to get right in systems that start and stop processes repeatedly. On Unix systems, the method for achieving a graceful shutdown of a process is to send it a signal (usually either SIGINT or SIGTERM).

The process can then register a handler for this signal which releases any resources that it is holding and exits.

The alternative to a graceful shutdown is a "forced termination". This is what happens when a process is unable to respond to a interrupt or termination signal. In this case, the process is ended immediately without the opprotunity to release any of the resources it holds.

On Unix systems, when you invoke process.kill() on a ChildProcess object, it sends the SIGTERM signal. That way, if the process has a SIGTERM handler it has an opportunity to perform a graceful shutdown.

Windows on the other hand, does not have a method to send signals to a process, so when you invoke the same process.kill() function on that platform, what you end up with is a forceful termination without any cleanup. However, Windows does have a method for applications to shutdown gracefully, namely hitting CTRL-C on the console to which the process is attached. And that's what this is all about. In fact, whenever you hit CTRL-C on a windows console that is running a node application, node will translate that CTRL-C into a SIGINT and dispatch any SIGINT handlers that it has registered. Since most well-behaved long-running Nodejs processes respond to SIGINT, this allows them to shutdown too.

This module exports a single ctrlc function which accepts a process id, and sends a CTRL-C event to that console.

Note: This is a no-op on non-windows platforms

Usage

import { ctrlc } from 'ctrlc-windows';

export function gracefulShutdown(process) {
  if (process.platform === 'win32') {
    ctrlc(process.pid);
  } else {
    process.kill();
  }
}

更新日志

ctrlc-windows

2.2.0

Minor Changes

  • 06dabc0: Added support for arm64.

2.1.0

Minor Changes

  • fa3cfb4: Converted to napi-rs, and copy the built files directly into the dist folder. Also switched to windows-rs which is being supported by Microsoft directly. This shouldn't cause any breaking changes, but has much of the internals updated to a new setup.

2.0.0

Major Changes

  • 29e0b68: Switch to using the n-api through the neon bindings. This provides more wholesale support for current and future versions of node, currently v10, v12, v14, and v16. (Previously ctrlc-windows@v1 only provided support for v10, v12 and v14.)

1.0.6

Patch Changes

  • 3298730: migrate to @mapbox/node-pre-gyp

1.0.5

Patch Changes

  • 696112a: Remove node 15 and 16 in release to undo broken 1.0.4

1.0.3

Patch Changes

  • 21ce6a2: Include the not-windows.js file to short circuit the build on non-windows platforms.
  • 3add0af: Add node 15 and 16 for the binary release workflow

1.0.2

Patch Changes

  • e67ba75: do not even invoke node-pre-gyp on non-windows platforms as part of the install script. This silences warnings that are printed to the console for npm

1.0.1

Patch Changes

  • 21ae36b: improved error messages when things go wrong

0.2.0

Minor Changes

  • 254b553:

0.1.3

Patch Changes

  • d6af1a8: Fix release workflow v2

0.1.2

Patch Changes

  • 0b0d6cc: Fix release workflow

0.1.1

Patch Changes

  • f66aae5: Release workflow