当我试图执行一个具有参数选项的可执行文件时。它将冻结nodejs的输出和输入,直到可执行文件关闭。不需要params的可执行文件只会运行,nodejs控制台不会冻结/锁定输入或输出。
参数为test.exe -thisisaparam
的示例。没有参数的示例:test.exe
。
下面是我的代码。(It a cli(
const cp = require('child_process');
let start = async function (start) {
let command = `start "" ${start}`;
cp.execSync(command);
console.log("Returning to menu in 10 seconds...")
setTimeout(() => {
run()
}, 10000);
};
以下是我如何调用函数。
async function startTest() {
await start("C:usersuserdownloadstest_param.exe")
}
谢谢,基弗。
使用execSync
运行的任何命令都将是同步的,这意味着它将等待命令退出,然后返回输出。
如果您不需要命令的输出,并且只想启动和分离,那么应该将spawn
与unref()
一起使用。
示例:
const scriptPath = "C:usersuserdownloadstest_param.exe"
cp.spawn('start', [scriptPath], {detached: true, stdio: 'ignore'}).unref()