内容简介:翻译自:https://stackoverflow.com/questions/35767918/node-js-how-to-spawn-detached-child-in-foreground-and-exit
根据 the docs
for child_process.spawn,我希望能够在前台运行子进程并允许节点进程本身退出,如下所示:
切换-exec.js:
'use strict';
var spawn = require('child_process').spawn;
// this console.log before the spawn seems to cause
// the child to exit immediately, but putting it
// afterwards seems to not affect it.
//console.log('hello');
var child = spawn(
'ping'
, [ '-c', '3', 'google.com' ]
, { detached: true, stdio: 'inherit' }
);
child.unref();
它只是退出而没有任何消息或错误,而不是看到ping命令的输出.
node handoff-exec.js hello echo $? 0
所以……在node.js(或根本没有)可以在父节点退出时在前台运行子节点吗?
更新:我发现删除console.log(‘hello’);允许孩子跑,但是,它仍然没有将前台标准控制传递给孩子.
你错过了
// Listen for any response:
child.stdout.on('data', function (data) {
console.log(data.toString());
});
// Listen for any errors:
child.stderr.on('data', function (data) {
console.log(data.toString());
});
你不需要child.unref();
翻译自:https://stackoverflow.com/questions/35767918/node-js-how-to-spawn-detached-child-in-foreground-and-exit
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML5和CSS3实例教程
Brian P.Hogan / 李杰、刘晓娜、柳靖、朱嵬 / 人民邮电出版社 / 2012-1 / 39.00元
《HTML5和CSS3实例教程》共分3部分,集中讨论了HTML5和CSS3规范及其技术的使用方法。首先是规范概述,介绍了新的结构化标签、表单域及其功能(包括自动聚焦功能和占位文本)和CSS3的新选择器。接下来是HTML对视频和音频的支持,讲述了画布上的图形绘制及CSS阴影、渐变和变换的使用方法。最后介绍使用HTML5的客户端特性(包括WebStorage、WebSQLDatabases以及离线支持......一起来看看 《HTML5和CSS3实例教程》 这本书的介绍吧!