Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 667 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nodejs Child Process: write to stdin from an already initialised process

#1
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.
Reply

#2
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
Reply

#3
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.

```js
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();
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through