简体   繁体   中英

How to kill node.exe child process?

I have the following piece of code with the "npm start" argument starting a node server instance :

const childProcess = require("child_process");
// running server before tests
before(function(done) {
childProcess.exec(["npm start"], function(err, out, code) {
if (err instanceof Error)
  throw err;
process.stderr.write(err);
process.stdout.write(out);
process.exit();
});

setTimeout(done, 5000);
});
//run tests
require("./customer-individual.js");
require("./customer-organization.js");

After tests run the node server instance is still running somewhere as a background process . How can i kill it ?

You can use the following:

const child = childProcess.exec(["npm start"], function(err, out, code) {
  // ...
});

child.kill(); // same as child.kill('SIGTERM');
console.log(child.killed); // will log true

Or any other signal, please refer to the docs: https://nodejs.org/api/child_process.html#child_process_child_kill_signal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM