1

I have a node.js script that runs another node.js script named 'app.js' using child_process.exec(). I need something that can terminate 'app.js'. Either a command that I can use in command line or something else in node.js.

I'm not trying to kill all node processes, only the process of 'app.js'. My computer is running Windows 10.

Right now the code looks like this:

let isLaunched = false;
const exec = require('child_process').exec;

function launch() {
    if (isLaunched) {
        isLaunched = false;

        // Something that terminates app.js

    } else {
        isLaunched = true;
        exec('node app.js');
    }
}

I call the function from a button in HTML.

<button onclick="launch()">Launch</button>

How can I do this in node.js?

1

1 Answer 1

1
var isLaunched = false;
var { exec } = require('child_process');
var myProcess; //use whatever name
function launch() {
    if (isLaunched) {
        isLaunched = false;
        myProcess.kill('SIGKILL');
    } else {
        isLaunched = true;
        myProcess = exec('node app.js');
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

sigkill can be replaced with sigterm or sigint, depending on what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.