简体   繁体   中英

Node.js Detached child process is invoked as interactive foreground process

I am invoking a detached child_process (in windows) like this

var stdout = fs.openSync('./out.log', 'a');
var stderr = fs.openSync('./out.log', 'a');

var child = child_process.spawn('node', ['./main.js'], {
    detached: true,
    stdio: [ 'ignore', stdout, stderr ],
    env: process.env
});

child.unref();

The spawned process, ie main.js in turn forks some workers using child_process.fork .

Normally the child and sub-children processes should run in background, correct me if I am wrong. But in when I try, the sub-children processes are running as interactively in foreground. I don't know what is causing them to behave like this. Could anyone point what could be the problem?

If I set detached to false , it works fine, but then the original parent can't exit without having all children exited.

I think this may be a problem with node on windows.

I tried both your code and the sample code on the node documentation you are for sure referencing yourself at http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options on my Ubuntu virtual machine and it works just fine. Process is detached and keeps running and there is no output anywhere but to out.log because it is running successfully in the background.

I ran both a simple ping command and also ran a node instance on a javascript file like you are trying to do and both worked just fine.

I would recommend you do the same with a ping command in windows. . .

var child = child_process.spawn('ping', ['-n','25','localhost'],. . . 

If that works like you would expect, there may be something inside your main.js that is causing the issue. If it doesn't work either, I would say you are up against a bug in node on windows and should report it.

Hope that helps some!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM