node.js:如何在前台生成分离的子节点并退出

栏目: Node.js · 发布时间: 7年前

内容简介:翻译自: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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编程风格

编程风格

[美] Cristina Videira Lopes / 顾中磊 / 人民邮电出版社 / 2017-8 / 55.00元

本书对一个常见的编程问题定义了不同的约束,分别使用33种方法实现了同一个词频统计任务,从而形成了风格迥异的编程风格。作者以惯用的计算机语言与简单的任务为画笔,描绘了一次生动难忘的编程之旅,帮助读者加深了对语言的理解,也提供了崭新的编程思路。一起来看看 《编程风格》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具