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();
Related
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?
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?
this is my previous code:-
const child_process = require('child_process');
child_process.exec(`java -cp ./${dirPath}bin Main`);
I tried to replace this like below:-
let spawn = require('child_process').spawn;
let child = spawn('java', [`-cp ./${dirPath}bin Main`]);
but I got error :-
"options" argument must be an object
How can I use spawn to execute java file by giving a specific path?
This works without an error in Node 10 on Windows:
let spawn = require('child_process').spawn;
let child = spawn('java', ['-version']);
Of course, this code throws away all output.
Also, there is an error in arguments, so your code should look like
let spawn = require('child_process').spawn;
let child = spawn('java', ['-cp', `./${dirPath}bin`, 'Main']);
To Konstantin's answer --> please be aware, that child_process package contained malicious code and was removed from the registry by the npm security team.
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.
I'm trying to create a node server that spawns phantomjs processes to create screenshots. The grab.js script works fine when executed and I've confirmed that it writes to stdout. Problem is the node code that spawns the process simply hangs. I've confirmed that phantomjs is in the path. Anyone know what might be happening here or how I might troubleshoot this?
Here's the phantomjs code (grab.js) that renders the page and writes the data to stdout:
var page = require('webpage').create(),
system = require('system'),
fs = require('fs');
var url = system.args[1] || 'google.com';
page.viewportSize = {
width: 1024,
height: 1200
};
page.open(url, function() {
var b64 = page.renderBase64('png');
fs.write('/dev/stdout', b64, 'w');
phantom.exit();
});
And here's the node code that spawns the phantom progress and prints the result (hangs):
var http = require('http'),
exec = require('child_process').exec,
fs = require('fs');
exec('phantomjs grab.js google.com', function(error, stdout, stderr) {
console.log(error, stdout, stderr);
});
I have had similar issues with exec and then switched to using spawn instead and it worked.
According to this article , Use spawn when you want the child process to return huge binary data to Node, use exec when you want the child process to return simple status messages.
hth
I had same problem, in my case it was not in nodejs, but in phantomjs (v2.1).
It's known problem when phantom`s open method hangs.
Also, found second link (I guess same person wrote) in which author points that requestAnimationFrame is not working well with tweenJs, which causes freezing. PhantomJS returns unixtimestamp but tweenjs expects it to be DOMHighResTimeStamp, and so on...
Trick is to inject request-animation-frame.js (which is also provided in that article)