I'm working on spawn
on nodejs. previously I was using exec
but exec has no compatibility in stdout (or stderr) steaming.
now, I have a disability about spawn. It seems whereas exec accepted quoted string command, spawn does not accepted that one, just array format.
so following exec script is working correctly but another one which uses spawn will be error, due to string formatted command not array.
const exec = require('child_process').exec;
exec(` echo foo bar `, (error, stdout, stderr) => {
if ( error ){ console.log(error) }
if ( stdout ){ console.log(stdout) }
if ( stderr ){ console.log(stderr) }
});
const { spawn } = require('child_process');
command = spawn('echo foo bar');
command.stdout.on('data', function (data) {
console.log(data.toString());
});
command.stderr.on('data', function (data) {
console.log( data.toString());
});
command.on('exit', function (code) {
console.log( code.toString());
});
I have a lot of command line script which is what I want to spawn
on nodejs, and all of them are complicated, something like following one. that's why I want to use string format for specify command rather than array. Any idea?
exec(' bash -ic "expecto \\"sudo bash -ic \\\\\\"rd ; backup_important_batch\\\\\\"\\" $PASSWORD" ', (error, stdout, stderr) => {
if ( error ){ console.log(error) }
if ( stdout ){ console.log(stdout) }
if ( stderr ){ console.log(stderr) }
});