hotfusion
hotfusion

Reputation: 45

How come the child process spawn works on window but not on ubuntu?

I am struggling running a command using spawn on ubuntu. I am using vrp-cli module written with Rust, here is the code where i am trying to pass few arguments into spawn and run the command:

const Process = require("child_process").spawn('vrp-cli', 'solve pragmatic vrps/problem.json  vrps/solution.json -g vrps/solution.geojson');

and I am getting an error:

The subcommand 'solve
pragmatic' wasn't recognized
        Did you mean 'solve'?
If you believe you received this message in error, try re-run
ning with 'vrp-cli -- solve
pragmatic'
USAGE:
    vrp-cli [SUBCOMMAND]
For more information try --help

But when I am running same code with one argument(on ubuntu):

const Process = require("child_process")('vrp-cli', '-h');

Its working...

Same line code with more than one argument i am running on windows and its working with no problems:

const Process = require("child_process").spawn(`cmd`,`/K`,'solve pragmatic vrps/problem.json  vrps/solution.json -g vrps/solution.geojson');

So why do you think providing one argument in spawn is working but when i have more than one argument i am getting an error? Some how on windows its working fine but not on ubuntu. Thank you

Upvotes: 2

Views: 558

Answers (1)

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

You need to follow the syntax for running command which have multiple arguments, for sample : 'ls -lrth /usr'

const { spawn } = require('child_process');
const outputLs = spawn('ls', ['-lrth', '/usr']);

outputLs.stdout.on('data', (data) => {
  console.log(data.toString());
});

outputLs.stderr.on('data', (data) => {
  console.error(data.toString());
});

outputLs.on('exit', (code) => {
  console.log(`Child exited with code ${code}`);
});

Also, first try to check the command normally on shell, does it work on Ubuntu systems? For your command, you should do something like this:

const { spawn } = require('child_process');
const outputLs = spawn('vrp-cli', ['solve', 'pragmatic', 'vrps/problem.json', 'vrps/solution.json', '-g', 'vrps/solution.geojson']);

outputLs.stdout.on('data', (data) => {
  console.log(data.toString());
});

outputLs.stderr.on('data', (data) => {
  console.error(data.toString());
});

outputLs.on('exit', (code) => {
  console.log(`Child exited with code ${code}`);
});

You need to pass all the arguments in an Array separated like above.

Upvotes: 1

Related Questions