Child Process
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}`);
});
默认情况下:stdin, stdout and stderr 这3个管道会链接在父进程和子进程之间!这使得父子进程数据流的交互畅通无阻。注意:有些程序自身内部利用了I/0
Buffer,但是这并不影响Node.js. 它只是意味着父进程发送数据给子进程不能马上消耗掉而已。
Nodejs为了方便大家使用,提供了很多同步与异步的方法,本文只介绍异步的!异步的优点就是不会阻塞Nodejs的事件循环。
child_process.exec(): spawns a shell and runs a command within that shell, passing thestdoutandstderrto a callback function when complete.child_process.execFile(): similar tochild_process.exec()except that it spawns the command directly without first spawning a shell.child_process.fork(): spawns a new Node.js process and invokes a specified module with an IPC communication channel established that allows sending messages between parent and child.child_process.execSync(): a synchronous version ofchild_process.exec()that will block the Node.js event loop.child_process.execFileSync(): a synchronous version ofchild_process.execFile()that will block the Node.js event loop.
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 argumentsoptions<Object>cwd<String> Current working directory of the child processenv<Object> Environment key-value pairsencoding<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-cswitch on UNIX or/d /s /con Windows. On Windows, command line parsing should be compatible withcmd.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])
file<String> The name or path of the executable file to runargs<Array> List of string argumentsoptions<Object>cwd<String> Current working directory of the child processenv<Object> Environment key-value pairsencoding<String> (Default:'utf8')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>
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的更多相关文章
- 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 ...
- innobackupex:Error:xtrabackup child process has died at /usr/bin/innobackupex
使用innobackupex进行数据库备份,报如下错误:innobackupex --compress --parallel=4 --user=root --password=yoon /expo ...
- nodejs child process
//Create child processvar thread = require('child_process'); var msg = thread.fork(__dirname + '/chi ...
- 【笔记】mongodb启动不了:child process failed, exited with error number 100
今天在启动mongodb的时候,发现起不来,报错:child process failed, exited with error number 100然后先去/var/log/mongo/mongod ...
- Wireshark:couldn't run dumpcap in child process(附带Linux下探索过程)
之前都是直接使用Kali里面安装好的Wireshark和Win下的,Ubuntu的来个小计 PS:解决方法不重要,我觉得更重要的是这个摸索的过程 解决方法 # 安装wireshark sudo apt ...
- Mongodb中经常出现的错误(汇总)child process failed, exited with error number
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 异常处理汇总-数据库系列 http://www.cnblogs.com/dun ...
- Child Process模块
目录 exec() execSync() execFile() spawn() fork() send() 参考链接 child_process模块用于新建子进程.子进程的运行结果储存在系统缓存之中( ...
- ERROR: child process failed, exited with error number 100
[root@localhost ~]# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --loga ...
- node中非常重要的process对象,Child Process模块
node中非常重要的process对象,Child Process模块Child Process模块http://javascript.ruanyifeng.com/nodejs/child-proc ...
随机推荐
- 通过yocto给p1010rdb定制linux,并启动linux
一.通过yocto定制linux 1.安装yocto yocto只能在非root用户下编译,所以先新建一个用户. useradd chen passwd -d chen 重启电脑进入chen用户. ...
- 傻瓜式硬盘重装win7系统图文加视频教程
标准适用环境: win7系统还能用,但是想重装win7系统.[当然win7进不去也可以在PE进行] 其它 需要工具: win7 ISO镜像 | [ w ...
- invesments 第三章 上
1. How firms issue securities: 公司如何发行股票 A. primary market: 新的股票,债券和其他的证券第一次发行的market B. ...
- WebGL 初探
官方网站:http://webglfundamentals.org/ WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenG ...
- 解决 VM虚拟机网卡无法拉起 的问题
复制虚拟机后,第二块网卡始终无法正常运作,表现为无法自动获得dhcp地址,且重启后与第一块网卡使用同样的地址,让人苦笑不得,反复重启了很多次都是这样,后面想到ifcfg-eth1的配置文件可能写得太简 ...
- JavaScript-打开新窗口
open()方法可以查找一个已经存在或者新建一个新的浏览器窗口. 语法:window.open([URL], [窗口名称], [参数字符串]) 参数解释: URL:可选参数,在窗口中显示网页的网址或路 ...
- Keepalived+Lvs+Mysql主主复制
一简单介绍 Keepalived+lvs+mysql主主复制是比較经常使用的一种Mysql高可用方案,当中lvs 提供读负载均衡,Keepalived通过虚拟vip漂移实现故障自己主动转移,而Mysq ...
- hdu1573X问题(不互素的中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- ExtJS4.x动态加载js文件
动态加载js文件是ext4.x的一个新特性,可以有效的减少浏览器的压力,提高渲染速度.如动态加载自定义组件 1.在js/extjs/ux目录下,建立自定义组件的js文件. 2.编写MyWindow.j ...