Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何终止node.exe子进程?_Node.js_Child Process - Fatal编程技术网

Node.js 如何终止node.exe子进程?

Node.js 如何终止node.exe子进程?,node.js,child-process,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.stder

我有一段带有“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");

测试运行后,节点服务器实例仍作为后台进程运行。如何杀死它?

您可以使用以下方法:

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

child.kill(); // same as child.kill('SIGTERM');
console.log(child.killed); // will log true
或任何其他信号,请参考文件: