如何在Node.js中杀死子进程? [英] How to kill childprocess in nodejs?

查看:1190
本文介绍了如何在Node.js中杀死子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用shelljs创建一个子进程

Created a childprocess using shelljs

!/usr/bin/env node

require('/usr/local/lib/node_modules/shelljs/global');
   fs = require("fs");  
   var child=exec("sudo mongod &",{async:true,silent:true});

   function on_exit(){
        console.log('Process Exit');
        child.kill("SIGINT");
        process.exit(0)
    }

    process.on('SIGINT',on_exit);
    process.on('exit',on_exit);

子进程杀死父进程后仍在运行..

Child process is still running .. after kill the parent process

推荐答案

如果可以使用内置的节点 child_process.spawn ,您就可以向子进程发送SIGINT信号:

If you can use node's built in child_process.spawn, you're able to send a SIGINT signal to the child process:

var proc = require('child_process').spawn('mongod');
proc.kill('SIGINT');

这样做的一个好处是,主进程应该一直徘徊,直到所有子进程都终止.

An upside to this is that the main process should hang around until all of the child processes have terminated.

这篇关于如何在Node.js中杀死子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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