nodejs child_process spawn command input file - node.js

I would like to run the following code in nodejs
$ ./a.out < inputfile.txt
So I wrote the following code.
var run = spawn('./a.out', ['< input.txt']);
var run = spawn('./a.out < input.txt');
I tried this, but it did not work.
What I want to do is to input input.txt ina.out
how can i do?

Since < (redirection) is a shell construct, you need to run your command line with a shell. That's what child_process.exec() is for:
const exec = require('child_process').exec;
exec('./a.out < inputfile.txt', function(err, stdout, stderr) {
if (err) {
return console.error('exec error:', err);
}
console.log('stdout:', stdout);
console.log('stderr:', stderr);
});

Related

Node.js Child process can not catch windows exe file's output

I want to use child_process.spawn to execute a windows exe file and catch it's output.
When I use command line to run a thirdparty exe file (says A.exe), it will print some logs to the cmd window. Like this:
C:\> A.exe
some outputs...
some more outputs...
However, when I spawn it in node.js, using this
import childProcess from 'child_process';
const cp = childProcess.spawn('A.exe');
cp.stdout.on('data', data => console.log(`stdout: ${data}`));
cp.stderr.on('data', data => console.log(`stderr: ${data}`));
There is no outputs at all.
I think the outputs of A.exe is not to the stdout (so I can never get data by listening stdout), but I don't know how it print logs when running from command line.
Any help would be greatly appreciated.
On Unix-type operating systems (Unix, Linux, macOS) child_process.execFile() can be more efficient because it does not spawn a shell. On Windows, however, .bat and .cmd files are not executable on their own without a terminal, and therefore cannot be launched using child_process.execFile(). When running on Windows, .bat and .cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the .bat or .cmd file as an argument (which is what the shell option and child_process.exec() do). In any case, if the script filename contains spaces it needs to be quoted.
// On Windows Only ...
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stderr.on('data', (data) => {
console.log(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
Maybe give this approach a go:
var childProcess = require('child_process');
childProcess.exec('A.exe', function(error, stdout, stderr) {
if (error != null) {
console.log('error occurred: ' + error);
} else {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
}
});
// OR
var cp = childProcess.spawn('A.exe');
cp.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString());
});
cp.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString());
});

Run Bash-Script within Electron App using child_process.exec

I'm struggling with running a bash-script within main.html.
const exec = require("child_process").exec;
// Execute bash script
exec("/c/workspace/_edu_zone/Proxy_Manager/filemover.sh", shellCallback);
// Callback
function shellCallback(error, stdout, stderr) {
console.log(error, stdout)
}
I'm always getting the error: no such file or directory. What am i doing wrong?
Any help is highly appreciated.
change
/c/workspace/_edu_zone/Proxy_Manager/filemover.sh
to
c:/workspace/_edu_zone/Proxy_Manager/filemover.sh
or
your could try using node-powershell to execute the command directly
const shell = require('node-powershell')
let ps = new shell({
executionPolicy: 'Bypass',
noProfile: true
});
function lunchnode() {
process.stdout.write('logging');
ps.addCommand('node run.js')
ps.invoke()
.then(function (output) {
process.stdout.write(output)
}).catch(function (err) {
process.stdout.write(err)
ps.dispose()
})
}

NodeJS exec() command for both Windows and Ubuntu

Using NodeJS, NPM, and Gulp.
I want to build a gulp task to run JSDoc that works on Ubuntu and Windows.
This works on Ubuntu...
var exec = require('child_process').exec;
return function(cb) {
exec('node node_modules/.bin/jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
And this works on Windows...
var exec = require('child_process').exec;
return function(cb) {
exec('node_modules\\.bin\\jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
Needless to say, neither works on the other. How do others solve this type of problem?
Try using path.resolve, which should provide you with a full path to the file regardless of the platform.
Node has process.platform, which... "returns a string identifying the operating system platform on which the Node.js process is running. For instance darwin, freebsd, linux, sunos or win32"
https://nodejs.org/api/process.html#process_process_platform
var exec = require('child_process').exec;
return function(cb) {
if (process.platform === 'win32') {
// Windows OS
} else {
// everything else
}
};
Using path.resolve:
const exec = require('child_process').exec;
const path = require('path');
return function(cb) {
let command = `node ${path.resolve('node_modules/.bin/jsdoc')} -c jsdoc-conf.json`;
exec(command, function(err, stdout, stderr) {
cb(err);
});
};

(Node.js) How to store stdout.pipe into a variable?

I want to get free -m (linux shell command) and store its result into a variable
by using source code below:
var spawn = require('child_process').spawn,
command = spawn('free', ['-m']);
command.stdout.pipe(process.stdout);
Is there any way to store process.stdout in a variable, please me some suggestions
This is fairly simple with child_process.exec:
var child = require("child_process");
var freeOut;
child.exec("free", ["-m"], function (error, stdout, stderr) {
if (error) {
console.error(error, stderr);
return;
}
//stdout and stderr are available here
freeOut = stdout;
process.stdout.write(stdout);
});
//Note. Do NOT use freeOut here. exec is async. Can only use in the callback

Run shell command and reply to questions

I've a shell script that, when started, asks for credentials.
I would like to start my script with Node.js using exec() and send the credentials when asked.
Currently my code is this:
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("myscript", puts);
Is it possible?
n.b. I can't pass the credentials as arguments of the script, it doesn't support them.
You should write to the stdin of the process. This is an example with exec:
var exec = require('child_process').spawn;
var child = spawn('script');
child.stdin.setEncoding('utf8');
child.stdout.on('data', function (data) {
if (data.toString() === 'enter the password:'){
child.stdin.write('pass');
return child.stdin.end();
}
console.log('got some other data:');
console.log(data.toString())
});
Similar to this question.

Resources