如何杀死node.exe子进程?

时间:2016-09-13 12:49:57

标签: node.js child-process

我有以下代码,其中“npm start”参数启动节点服务器实例:

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");

运行测试后,节点服务器实例仍在某处作为后台进程运行。我怎么能杀了它?

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

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

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

或任何其他信号,请参阅文档:https://nodejs.org/api/child_process.html#child_process_child_kill_signal