简体   繁体   English

如何使用pid杀死Node.js子进程?

[英]How to kill Node.js child process using pid?

I am looking for a way to kill Node.js processes using their pid.我正在寻找一种使用 pid 杀死 Node.js 进程的方法。 I've been searching Google, StackOverflow, and the Node.js documentation for hours.我一直在搜索 Google、StackOverflow 和 Node.js 文档几个小时。 It seems like something that should exist.似乎是应该存在的东西。 I can only find how to do it based on my variable newProc below, but I cannot do newProc.kill() because I want to kill the child process from outside of the function scope.我只能根据下面的变量newProc找到如何执行此newProc ,但是我不能执行newProc.kill()因为我想从函数范围之外杀死子进程。 Additionally, I need to store something in MongoDB for record-keeping purposes, so I thought pid would be more appropriate.此外,我需要在 MongoDB 中存储一些东西以进行记录保存,所以我认为 pid 会更合适。

var pid = newJob();
kill(pid); //<--anything like this?

var newJob = function () {
  var exec = require('child_process').exec,
  var newProc = exec("some long running job", function (error, stdout, stderr) {
    console.log("stdout: " + stdout);
    console.log("stderr: " + stderr);
    if (error) console.log('exec error: ' + error);
  });
  return newProc.pid;
}

EDIT: Can I use process.kill(pid) on a child process?编辑:我可以在子进程上使用process.kill(pid)吗? The docs state that it is a global object that can be accessed from anywhere. 文档声明它是一个可以从任何地方访问的全局对象。

Finally, as a slight tangent, is there a way to ensure that the pid's will always be unique?最后,稍微说一下,有没有办法确保 pid 始终是唯一的? I don't want to unintentionally kill a process, of course.当然,我不想无意中杀死一个进程。

process.kill() does the job. process.kill()完成这项工作。 As said in your edit, process is a global object available in every Node module.正如您在编辑中所说, process是每个 Node 模块中可用的全局对象。

Note that the PID only uniquely identifies your child process as long as it is running.请注意,PID 仅唯一标识您的子进程,只要它正在运行。 After it has exited, the PID might have been reused for a different process.退出后,PID 可能已重新用于不同的进程。 If you want to be really sure that you don't kill another process by accident, you need some method of checking whether the process is actually the one you spawned.如果您想真正确定不会意外杀死另一个进程,则需要某种方法来检查该进程是否实际上是您生成的进程。 ( newProc.kill() is essentially a wrapper around process.kill(pid) and thus has the same problem .) newProc.kill()本质上是process.kill(pid)的包装器,因此有同样的问题。)

You can use the module "tree-kill".您可以使用模块“tree-kill”。

var kill  = require('tree-kill');
var exec  = require('child_process').exec;

  var child = exec('program.exe',  {cwd: 'C:/test'}, (err, stdout, stderr) => {
        if (stdout) console.log('stdout: ' + stdout);
        if (stderr) console.log('stderr: ' + stderr);
        if (err !== null) {
          console.log('exec error: ' + err)};
    });
  child.stdout.on('data', function(log_data) {
      console.log(log_data)
  });

kill(child.pid);

This kill you child process by it PID for sure ;-)这肯定会通过 PID 杀死您的子进程;-)

use tree-kill module and pass the id as process.pid which kills the processes which are long and doesn't return .使用 tree-kill 模块并将 id 作为 process.pid 传递,这会杀死很长且不返回的进程。

const kill  = require('tree-kill');
const exec  = require('child_process').exec;

exec("some long non-return job like server start", function (error, stdout, stderr) {
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
if (error) console.log('exec error: ' + error);
});       
kill(process.pid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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