Child Process

child_process 这个模块可以生成一个子进程。nodejs提供了好几个API,本质上都是调用child_process.spawn():

 const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
}); ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
}); ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});

默认情况下:stdinstdout and stderr 这3个管道会链接在父进程和子进程之间!这使得父子进程数据流的交互畅通无阻。注意:有些程序自身内部利用了I/0

Buffer,但是这并不影响Node.js. 它只是意味着父进程发送数据给子进程不能马上消耗掉而已。

Nodejs为了方便大家使用,提供了很多同步与异步的方法,本文只介绍异步的!异步的优点就是不会阻塞Nodejs的事件循环。

child_process.spawn()child_process.fork()child_process.exec(), and child_process.execFile()

都会返回一个ChildProcess 实例。ChildProcess 实现了NodejsEventEmitter API,允许父进程注册监听函数,在子进程的生命周期内发生指定事件的时候调用。

child_process.exec() and child_process.execFile()还额外提供了callback选项,当子进程终止的时候调用!

Spawning .bat and .cmd files on Windows

child_process.exec() and child_process.execFile()最大的区别无疑是基于的平台不同!

在 Unix-type 这样类型的操作系统上 (Unix, Linux, OSX) ,child_process.execFile() 跑起来更高效,因为他不需要生成一个shell.

然后在Windows上,.bat and .cmd文件是无法离开终端独立执行的,玩Windows的同志只能用child_process.exec()来执行批处理文件。

或者说利用child_process.spawn() 配置项里设置shell.

或者说利用生成一个cmd.exe ,然后传 .bat or .cmd文件名作为参数。这里关键了。一定要带上 /? 不然在你无法生成一个新的shell实例。

 // On Windows Only ...
const spawn = require('child_process').spawn;
const bat = spawn('cmd.exe', ['/c', 'my.bat']); bat.stdout.on('data', (data) => {
console.log(data);
}); bat.stderr.on('data', (data) => {
console.log(data);
}); bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
}); // OR...
const exec = require('child_process').exec;
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
}); // Script with spaces in the filename:
const bat = spawn('"my script.cmd"', ['a', 'b'], { shell:true });
// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
// ...
});

child_process.exec(command[, options][, callback])

  • command <String> The command to run, with space-separated arguments
  • options <Object>
    • cwd <String> Current working directory of the child process
    • env <Object> Environment key-value pairs
    • encoding <String> (Default: 'utf8')
    • shell <String> Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /d /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)
    • timeout <Number> (Default: 0)
    • maxBuffer <Number> largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (Default: 200*1024)
    • killSignal <String> (Default: 'SIGTERM')
    • uid <Number> Sets the user identity of the process. (See setuid(2).)
    • gid <Number> Sets the group identity of the process. (See setgid(2).)
  • callback <Function> called with the output when process terminates
  • Returns: <ChildProcess>
 {
encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
}

注意回调里的3个参数,如果指定了encoding,那就是String,不然就是buffer。

timeout(如果大于0) 是为了规定子进程的执行时间,如果超过了,这时候就会用到 killSignal 属性了,默认父进程发送的是 'SIGTERM'。

不同于exec(3) POSIX,child_process.exec()    是不会取代已经存在的进程的,使用shell来执行命令。

 

child_process.execFile(file[, args][, options][, callback])

child_process.execFile()和child_process.exec()  很像,除了 child_process.execFile() 执行的时候不会生成新的shell,这个指定的file将会被执行作为新进程。

这一点比child_process.exec() 更有效率。

这里部分一点:

开始学习child_process模块的时候以为spawn可以直接运行命令, 后来发现这是一个小陷阱就拿出来和大家分享一下.

先说下我碰到的情况由于在windos下写的所以根据docs上的例子我就写出了这么一句代码:"require(“child_process”).spawn(“dir”), 这么写是会有错误的,用error接受到的数据是没有此文件. 而用exec就不会有问题,于是得到了以前的猜想.

大家都知道在linux下, ls命令对应的是一个文件, 而在windows下是做为cmd的内置命令的. 所以像我那样写是会报错.

于是我查看了child_process的源码发现spawn是这样定义的var spawn = exports.spawn = function(file, args, options); 也就是说他传入的应该是一个文件, 例如ping, cmd等. 而exec的源码中有一段这样的代码:

 if (process.platform === 'win32') {
file = 'cmd.exe';
args = ['/s', '/c', '"' + command + '"'];
// Make a shallow copy before patching so we don't clobber the user's
// options object.
options = util._extend({}, options);
options.windowsVerbatimArguments = true;
} else {
file = '/bin/sh';
args = ['-c', command];
}

所以想使用内置命令可以直接使用exec或者把spawn改成spawn(“cmd.exe”,["\s", “\c”, “dir”]);

总结起来就是spawn是调用一个文件! 不要被docs上的child_process.spawn(command, [args], [options])中的command给骗了

-----------------------------------------------------------------------------------------------

options.detached

在windows上,设置options.detached 为 true,可以保证父进程退出的时候,子进程还可以运行,子进程拥有自己的console窗口,一旦启动,就不可能停止。

非windows的话,设置options.detached 为 true,子进程将会新进程的控制者,不管父子进程有没有设置detached ,子进程都可以在父进程退出后保存运行!

默认情况下 ,父进程需要等子进程运行完毕才离开,但是我们可以调用child.unref() 来避免发生!这样允许父进程和子进程独立开来,除非他们建立了IPC信道。

看一眼为什么:

针对handle而言,判断loop是否存活只要看loop->active_handles是否大于0,大于0则存活。

具体代码参看 https://github.com/libuv/libuv/blob/v1.x/src/uv-common.h

uv__handle_init, uv__handle_start, uv__handle_stop, uv__handle_ref, uv__handle_unref

比较下面几种情况,可能会有利于理解unref的作用。

第一种

var timer1 = setTimeout(function(){
console.log(new Date, 1);
}, 1000);
// setTimeout=>uv_timer_start(timer1) active_handles = 1 var timer2 = setInterval(function(){
console.log(new Date, 2);
}, 1000);
// setInterval=>uv_timer_start(timer2) active_handles = 2 // 1: ative_handles > 0 => loop()
// timer1 timeout => uv_timer_stop(timer1) active_handles = 1 => callback()
// timer2 timeout => uv_timer_stop(timer2) active_handles = 0 => callback() => uv_timer_start(timer2) active_handles = 1
// 2: active_handles > 0 => loop()
// timer2 timeout => uv_timer_stop(timer2) active_handles = 0 => callback() => uv_timer_start(timer2) active_handles = 1
// goto 2

第二种

var timer1 = setTimeout(function(){
console.log(new Date, 1);
}, 1000);
// setTimeout=>uv_timer_start(timer1) active_handles = 1 var timer2 = setInterval(function(){
console.log(new Date, 2);
}, 1000);
// setInterval=>uv_timer_start(timer2) active_handles = 2 timer2.unref();
// uv_unref(timer2) active_handles = 1 // ative_handles > 0 => loop()
// timer1 timeout => uv_timer_stop(timer1) active_handles = 0 => callback()
// timer2 timeout => uv_timer_stop(timer2) active_handles = 0 => callback() => uv_timer_start(timer2) active_handles = 0
// active_handles == 0 => exit_process

第三种

var timer1 = setInterval(function(){
console.log(new Date, 1);
}, 1000);
// setInterval=>uv_timer_start(timer1) active_handles = 1 var timer2 = setInterval(function(){
console.log(new Date, 2);
}, 1000);
// setInterval=>uv_timer_start(timer2) active_handles = 2 // 1: ative_handles > 0 => loop()
// timer1 timeout => uv_timer_stop(timer1) active_handles = 1 => callback() => uv_timer_start(timer1) active_handles = 2
// timer2 timeout => uv_timer_stop(timer2) active_handles = 1 => callback() => uv_timer_start(timer2) active_handles = 2
// goto 1

第四种

var timer1 = setInterval(function(){
console.log(new Date, 1);
}, 1000);
// setInterval=>uv_timer_start(timer1) active_handles = 1 var timer2 = setInterval(function(){
console.log(new Date, 2);
}, 1000);
// setInterval=>uv_timer_start(timer2) active_handles = 2 timer2.unref()
// uv_unref(timer2) active_handles = 1 // 1: ative_handles > 0 => loop()
// timer1 timeout => uv_timer_stop(timer1) active_handles = 0 => callback() => uv_timer_start(timer1) active_handles = 1
// timer2 timeout => uv_timer_stop(timer2) active_handles = 1 => callback() => uv_timer_start(timer2) active_handles = 1
// goto 1

第五种

var timer1 = setInterval(function(){
console.log(new Date, 1);
}, 1000);
// setInterval=>uv_timer_start(timer1) active_handles = 1 timer1.unref()
// uv_unref(timer1) active_handles = 0 var timer2 = setInterval(function(){
console.log(new Date, 2);
}, 1000);
// setInterval=>uv_timer_start(timer2) active_handles = 1 timer2.unref()
// uv_unref(timer2) active_handles = 0 // ative_handles == 0 => exit process

--------------------------------------------------------------------------

最后总结:4个方法其实大同小异,最后其实都是调用spawn,需不需要用shell,完全看你的file是不是需要在shell 上运行,最后注意一下

maxBuffer and Unicode

这个属性最好你别设置,stdout,stdin stderr里的buffer量超出了,child process就鸡鸡了。

Child Process的更多相关文章

  1. wireshark_Couldn’t run /usr/sbin/dumpcap in child process: Permission denied

    关于Wireshark出现:Couldn't run /usr/sbin/dumpcap in child process: Permission denied Are you a member of ...

  2. innobackupex:Error:xtrabackup child process has died at /usr/bin/innobackupex

    使用innobackupex进行数据库备份,报如下错误:innobackupex --compress --parallel=4  --user=root  --password=yoon /expo ...

  3. nodejs child process

    //Create child processvar thread = require('child_process'); var msg = thread.fork(__dirname + '/chi ...

  4. 【笔记】mongodb启动不了:child process failed, exited with error number 100

    今天在启动mongodb的时候,发现起不来,报错:child process failed, exited with error number 100然后先去/var/log/mongo/mongod ...

  5. Wireshark:couldn't run dumpcap in child process(附带Linux下探索过程)

    之前都是直接使用Kali里面安装好的Wireshark和Win下的,Ubuntu的来个小计 PS:解决方法不重要,我觉得更重要的是这个摸索的过程 解决方法 # 安装wireshark sudo apt ...

  6. Mongodb中经常出现的错误(汇总)child process failed, exited with error number

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 异常处理汇总-数据库系列  http://www.cnblogs.com/dun ...

  7. Child Process模块

    目录 exec() execSync() execFile() spawn() fork() send() 参考链接 child_process模块用于新建子进程.子进程的运行结果储存在系统缓存之中( ...

  8. ERROR: child process failed, exited with error number 100

    [root@localhost ~]# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --loga ...

  9. node中非常重要的process对象,Child Process模块

    node中非常重要的process对象,Child Process模块Child Process模块http://javascript.ruanyifeng.com/nodejs/child-proc ...

随机推荐

  1. 用fiddler测试ip轮询

    测试业务: 服务端根据域名配置了三台服务器ip,测试ip轮询的逻辑 测试方法: 使用fiddler配置hosts即可 1.1.1.1 第一台ip 1.1.1.1 第二台ip 1.1.1.1 第三台ip ...

  2. joseph-约瑟夫环问题

    约瑟夫环运作如下: 1.一群人围在一起坐成环状(如:N) 2.从某个编号开始报数(如:K) 3.数到某个数(如:M)的时候,此人出列,下一个人重新报数 4.一直循环,直到所有人出列,约瑟夫环结束 关于 ...

  3. 华为IC设计人员的薪酬(5年经验28万),以及麒麟是如何脱颖而出的~

    垂直整合助力麒麟腾飞 由于ARM技术路线大幅降低了技术门槛和研发的资金和时间成本,导致ARM阵营参与者众多,加上大家都是购买ARM的CPU核与GPU核,造成产品高度同质化,市场竞争异常激烈——在价格上 ...

  4. Linux系统编程(9)—— 进程之进程控制函数exec系列函数

    在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; int exe ...

  5. 2014第3周三JS进阶书籍

    本来想尝试每天回答或看已解决的3个问题来学习总结今天的知识点,看了下博文里面的问答,在问的和已解决的都提不起兴趣.就看了下知识库里面一些文章,把里面感觉好的段落再摘录一下,为自己再看时备忘. 第一阶段 ...

  6. WIN7 64位系统安装JDK并配置环境变量

    本文来自:http://jingyan.baidu.com/article/3c343ff70bc6ea0d377963df.html 工具/原料 JDK 方法/步骤   首先,下载JDK安装包,到官 ...

  7. Android系统的开机画面显示过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7691321 好几个月都没有更新过博客了,从今天 ...

  8. Android应用程序组件Content Provider的启动过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6963418 通过前面的学习,我们知道在Andr ...

  9. mysql1主多从配置

    mysql一主多从的配置: 其实1主多从的配置与一主一从配置非常相似,现在主要讲讲一主多从的大概配置方法. 一, 1,master端开启binlog日志,并且设置server id=1. 2.重启服务 ...

  10. CheckStyle插件

    如今代码静态检查越来越重要,已经成为构建高质量软件的不可或缺的一个验证步骤.如果你使用的是java语言,那么CheckStyle则是一个利器. CheckStyle能够帮助程序员检查代码是否符合制定的 ...