0

I'm starting a process in a node app, running on Windows, which I cannot kill. I'm using node's exec which requires a command line parameter (thus I cannot use spawn). Normally this process runs without ending, expecting a Ctrl+C from the user to end it. I want to instead programmatically end it from within node.

For a simplified example of the scenario:

import { exec } from 'child_process'; // (Note that this is an ESM/module type file)
// Start the process (in this example, it runs a local server)
const serverProcess = exec('cordova run browser --port=1234');
// Wait a bit, and then try to kill the process
setTimeout(() => {
    serverProcess.kill();
    // also tried: process.kill(serverProcess.pid);
    console.log(serverProcess.killed);
    // Will log as true, but the process continues to run,
    // requiring Ctrl+C on the node program
}, 5000);

Edit: I have tried using different signals for kill, including setting killSignal in the options, but that has not remedied the problem. According to the node docs:

On Windows, where POSIX signals do not exist, the signal argument will be ignored, and the process will be killed forcefully and abruptly (similar to 'SIGKILL').

Luke
  • 18,811
  • 16
  • 99
  • 115
  • The default `killSignal` is `SIGTERM` (which can be intercepted by the process). (Note this is not the same as `SIGINT` which is sent by ctrl + c in a TTY.) Try setting `killSignal` to `SIGKILL` (which cannot be ignored by the process). Read more about signals at [this question](https://stackoverflow.com/q/4042201/438273). – jsejcksn Oct 05 '22 at 14:29
  • @jfriend00 - That was a typo that I've fixed. – Luke Oct 05 '22 at 18:36
  • @jsejcksn - Changing kill signals did not help. I added an edit to the question explaining this. – Luke Oct 05 '22 at 18:36
  • 1
    Not sure why you say you cannot use `spawn`, but it definitely takes command line args as well: `spawn("cordova", ["run", "browser", "--port=1234"])` – smac89 Oct 05 '22 at 18:42
  • Also which process keeps running? The `node` process or the `cordova` process? – smac89 Oct 05 '22 at 18:45
  • Tangent to your question: you'll probably find it quite helpful to develop using [containers](https://hub.docker.com/search?q=cordova). – jsejcksn Oct 05 '22 at 18:46

0 Answers0