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

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

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


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

查看所有标签

猜你喜欢:

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

大演算

大演算

佩德羅.多明戈斯 / 張正苓,胡玉城 / 三采 / 2016-8-1 / 620

揭開大數據、人工智慧、機器學習的祕密, 打造人類文明史上最強大的科技——終極演算法! 有一個終極演算法,可以解開宇宙所有的祕密, 現在大家都在競爭,誰能最先解開它! .機器學習是什麼?大演算又是什麼? .大演算如何運作與發展,機器可以預測什麼? .我們可以信任機器學過的東西嗎? .商業、政治為什麼要擁抱機器學習? .不只商業與政治,醫學與科學界也亟需......一起来看看 《大演算》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

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

HEX HSV 互换工具