我在ubuntu上使用spawn运行命令时遇到了困难。我正在使用
vrp-cli
使用Rust编写的模块,下面是我试图将一些参数传递给spawn并运行命令的代码:
const Process = require("child_process").spawn('vrp-cli', 'solve pragmatic vrps/problem.json vrps/solution.json -g vrps/solution.geojson');我得到了一个错误:
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但是当我运行相同的代码时
一个论点
(在ubuntu上):
const Process = require("child_process")('vrp-cli', '-h');它起作用了..。
我在windows上运行的带有多个参数的同一行代码,它的工作没有任何问题:
const Process = require("child_process").spawn(`cmd`,`/K`,'solve pragmatic vrps/problem.json vrps/solution.json -g vrps/solution.geojson');那么,为什么你认为在spawn中提供一个参数是可行的,但是当我有多个参数时,我会得到一个错误?有些在windows上是如何工作的,但在ubuntu上就不行了。谢谢你
发布于 2021-03-02 00:50:05
您需要遵循运行具有多个参数的命令的语法,例如:“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}`);
});另外,首先尝试在shell上检查该命令是否正常,它在Ubuntu系统上工作吗?对于您的命令,您应该这样做:
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}`);
});你需要像上面那样在一个数组中传递所有的参数。
https://stackoverflow.com/questions/66425795
复制相似问题