node child process: will it always terminate by itself?

一笑奈何 提交于 2021-02-18 08:15:19

问题


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}`);
});

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

My question is:

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

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

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


回答1:


  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.

  2. 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.

  3. 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.

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>.

Hope this helps.



来源:https://stackoverflow.com/questions/39523422/node-child-process-will-it-always-terminate-by-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!