Why child_process.spawn prevents parent exit? - node.js

I wrote a python script containing infinite loop (called it test.py).
And then I run it using spawn in nodeJS.
Here is the content of the nodejs Script:
const {spawn} = require("child_process")
spawn("python", ["test.py"], { encoding: 'utf8'});
Why this doesn't exit immediately?
Does the spawn put something in the Event Table or Poll Queue?

Related

node.js child_process spawn repl

Nodejs Child Process: write to stdin from an already initialised process
I saw this link, so I try like this :
const { spawn } = require('child_process');
const child = spawn('node');
child.stdin.setDefaultEncoding('utf-8');
child.stdout.pipe(process.stdout);
child.stdin.cork();
child.stdin.write("10+20\n");
child.stdin.uncork();
but this code does not output anything, so what should I do?

nodejs child_process spawned a subprocess, the subprocess not show console window

I write nodejs code, use child_process.spawn() method create a subprocess, but the subprocess does not show its console window, how to show it?
'use stricts';
let process = require('process');
let child_process = require('child_process');
var subprocess = child_process.spawn(
'CSharpProg.exe', [] ,
{
windowsHide: false
});
subprocess.stdout.on('data', d => console.log(d.toString('utf8')));
process.stdin.on('data', d => subprocess.stdin.write(d));
I expec the spawned subprocess can have its own console window shown.
In order to make subprocess show its own console window (on Windows environment), you need to set option shell and detached as true. The default value of option windowsHide is false, so it's not necessary to set it again.
I tested the following code and it works on Windows 10 with Node.js v10.16.0:
var subprocess = child_process.spawn('node', ['test2.js'], {
shell: true,
detached: true
});

Node Spawn process outliving the main node process

I want to start a process that will live on its own and continue to live even if the node application that started it dies.
To do so I am trying to use child_process and I did not manage to have the process live even if the node process die.
Here is my code:
const cp = require('child_process');
const process = cp.spawn('long_running_process', ['arg1'], {
stdio: 'ignore',
detached: true
});
process.unref();
This code follows the child_process documentation available here :
https://nodejs.org/api/child_process.html#child_process_options_detached

nodejs .execSync will not return data when calling from within a child forked process

I have my main.js
doing the following:
const fork = require('child_process').fork;
fork(myprocess........)
This all works fine....
Now inside the myprocess.js
const execSync = require('child_process').execSync;
var foo = execSync('myspecialprogram')
console.log(foo.toString());
Result of foo is empty buffer.
How do I get the execSync to return data from within the forked child process?
anyway, i think its actually workign fine, somehow the spawn process im running was retunring empty value. So its fine.

Nodejs Child Process: write to stdin from an already initialised process

I am trying to spawn an external process phantomjs using node's child_process and then send information to that process after it was initialized, is that possible?
I have the following code:
var spawn = require('child_process').spawn,
child = spawn('phantomjs');
child.stdin.setEncoding = 'utf-8';
child.stdout.pipe(process.stdout);
child.stdin.write("console.log('Hello from PhantomJS')");
But the only thing I got on the stdout is the initial prompt for phantomjs console.
phantomjs>
So it seems the child.stdin.write is not making any effect.
I am not sure I can send additional information to phantomjs ater the initial spawn.
You need to pass also \n symbol to get your command work:
var spawn = require('child_process').spawn,
child = spawn('phantomjs');
child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);
child.stdin.write("console.log('Hello from PhantomJS')\n");
child.stdin.end(); /// this call seems necessary, at least with plain node.js executable
You need to surround your write by cork and uncork, the uncork method flushes all data buffered since cork was called. child.stdin.end() will flush data too, but no more data accepted.
var spawn = require('child_process').spawn,
child = spawn('phantomjs');
child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);
child.stdin.cork();
child.stdin.write("console.log('Hello from PhantomJS')\n");
child.stdin.uncork();

Resources