节点子进程:它将始终自行终止吗? [英] node child process: will it always terminate by itself?

查看:56
本文介绍了节点子进程:它将始终自行终止吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码来生成子进程:

I have code to spawn child process like this:

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr'], {
    detached: true
});

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls..on('error', (data) => {
  console.log(`error: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

到目前为止,我可以从stdout和stderr控制台消息,它在完成任务后自动退出.

Till now, I can console message from stdout and stderr, and it exited automatically after finish the task.

我的问题是:

  1. 在什么情况下会发生错误"事件?

  1. in what situation, it will go to 'error' event?

子进程是否有可能无法终止?如果是这样,什么时候会发生?

is it possible that child process can't terminate? If so, when could that happen?

如果情况2发生了,我的主进程已经终止时如何杀死这些子进程?

if case 2 happens, how can I kill these child process when my main process already terminated?

推荐答案

  1. 每当 ls 命令时,子进程将出错,或者进程出错.因此,如果/user 不存在,则该过程将出错,然后消失.当您在Node中生成子进程时,其行为方式与在命令行中执行命令的方式相同.大多数进程将因错误而死亡.这真的取决于.您将必须阅读l​​inux或unix或正在使用的任何操作系统的文档,以查看发生错误时将发生的情况.但是,这些系统级命令应该消失.对于像 ls 这样的简单命令,它是非常安全的.如果错误,则死亡.结束.

  1. The child process will error whenever the ls command, or process errors. So if /user didn't exist, then the process would error and then die. When you spawn a child process in Node, it will behave in the same way as if you had executed the command in command line. Most processes will die on error. It really just depends. You would have to read documentation for linux or unix or whatever OS you're using to see what would happen when errors occur. But these system level commands should die For simple commands like ls though, it's pretty safe. If it errors, it dies. The end.

子进程可能不会终止.很难确切说明子进程何时不会终止.各种各样的事情都可能导致这种情况.例如,您分叉了某个进程但从未关闭它,或者子进程由于某种原因陷入了某种无限循环.虽然某些过程已经启动,并且可以无限期地继续下去,但只能手动关闭.

It's possible that a child process doesn't terminate. It's hard to say exactly when a child process wouldn't terminate. Any variety of things could cause this. For example, you forked some process but never closed it, or if the child process got stuck in some kind of infinite loop for some reason. Some processes though are started and are meant to continue indefinitely, only to be closed manually.

如果情况2曾经发生,您有几种方法可以杀死它.在该示例中,ls javascript变量引用表示该过程的对象.出于好奇,您可以将 ls 对象记录到命令行并查看其中的所有属性.因此,通常来说,每当您调用 spawn()时,它将返回给您这种子流程对象.在Node中,杀死进程的最简单方法是在子进程对象上调用 kill()函数.因此,在这种情况下,您将调用 ls.kill(signal)对于信号,可以使用'SIGINT''SIGTERM',而且我认为'SIGHUP'也可以.您可以在此处了解更多有关信号的信息.

If case 2 ever happens, you have a few ways to kill it. In the example, the ls javascript variable refers to an object that represents the process. For curiosity sake, you can log the ls object to the command line and see all of the properties in there. So in general, whenever you call spawn(), it returns to you this kind of child process object. In Node, the easiest way to kill the process is to call the kill() function on the child process object. So in this case, you would call ls.kill(signal) For the signal, you could use 'SIGINT', 'SIGTERM', and I think 'SIGHUP' works too. You can read more about signals here.

但是,您询问有关在主进程已经终止的情况下如何终止子进程的问题.为此,您必须使用命令行.如果您知道流氓子进程的进程ID,则只需运行 kill< pid> ,其中pid是进程ID.您可以从子流程对象获取流程ID.如果您不知道该pid并且无法获取该pid,则可以运行 ps -eaf ,它将打印出计算机上所有当前正在运行的进程的列表,很多.因此,为了帮助您找到所需的过程,可以执行 ps -eaf |grep< process-name> ,这可能会帮助您找到进程.因此,例如,如果在这种情况下 ls 命令没有消失(实际上几乎不会发生),我可以执行 ps -eaf |grep ls ,那么很可能会找到正确的进程和进程ID.然后,您可以运行 kill< pid> .

However, you asked about how to kill the child process if the main process has already terminated. In order to do that you have to use the command line. If you know the process id of the rogue child process, you can simply run kill <pid>, where pid is the process id. You can get the process id from the child process object. If you don't know the pid and can't get it, then you can run ps -eaf, which will print out a list of all currently running processes on your computer, which is a lot. So to help find the process your looking for, you can do ps -eaf | grep <process-name> and that might help you find the process. So for example, if in this case the ls command didn't die, which practically wouldn't happen, I could do ps -eaf | grep ls, and that would most likely find the right process and process id. Then you can run kill <pid>.

希望这会有所帮助.

这篇关于节点子进程:它将始终自行终止吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆