Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spawn new child process with own console window

Tags:

node.js

spawn

I've got a parent application in node.js which needs to spawn multiple worker applications (also in node.js) applications according to need.

I've already got communication working between them - don't need to use any of the built-in node stuff.

Now the problem is that I'd like each worker process to have it's own console window - since I do a lot of writing to the console and I want to keep an eye on it.

I've looked through the Node child_process documentation, and it says that by setting options to detached:

On Windows, setting options.detached to true makes it possible for the child process to continue running after the parent exits. The child will have its own console window.

However when I use my own code

const Process = require("child_process").spawn;
Process(process.argv[0], ["myApplicationPath","otherArgs"],{detached: true,stdio: ['ignore']});

It doesn't work. The child application does spawn, but no console window turns up.

like image 578
user2110845 Avatar asked May 18 '16 06:05

user2110845


1 Answers

I'm a bit late here, but I just had to figure this out as well, so here is the answer for anyone else who is struggling with this:

I managed to spawn my child application in its own console using this:

childProcess.spawn("<cmd>", [], {shell: true, detached: true});

In addition to the {detached: true} what OP is using, I used {shell: true}. With the combination of both, I managed to spawn my child application with its own console.

like image 70
Sossenbinder Avatar answered Sep 28 '22 03:09

Sossenbinder