a.bat
file is:
.bat文件为:
@echo off
start "" notepad.exe
echo foobar
Running cmd /c a.bat
in the command prompt opens the Notepad window.
And foobar
is printed before closing the window.
I can run a next command without closing Notepad.
在命令提示符下运行cmd/c a.bat将打开记事本窗口。并且在关闭窗口之前打印Foobar。我可以在不关闭记事本的情况下运行下一个命令。
However, in node.js child_process.execSync
, the execSync
function does not end until the
window is closed.
In the following code, the result of console.log is output after the window is closed.
但是,在node.js child_process.execSync中,execSync函数在窗口关闭之前不会结束。在下面的代码中,console.log的结果在窗口关闭后输出。
console.log(child_process.execSync(`cmd /c a.bat`))
Why does this happen?
Is it possible to get around it without using asynchronous functions like exec or spawn?
这一切为什么要发生?有没有可能在不使用诸如EXEC或SPOWN这样的异步函数的情况下绕过它?
更多回答
What happens if you use exec
rather than execSync
?
如果您使用exec而不是execSync,会发生什么?
If I replace execSync with exec, ChildProcess
instance is printed to the console immediately.
如果我将execSync替换为exec,ChildProcess实例将立即打印到控制台。
Is this what you expect, or would you like to achieve something else?
这是你所期望的吗,或者你想要取得其他成就?
优秀答案推荐
I got expected result by
我得到了预期的结果
console.log(cp.execSync(`cmd /c a.bat`, { stdio: "inherit" }))
// or
console.log(cp.execSync(`cmd /c a.bat`, { stdio: "ignore" }))
更多回答
我是一名优秀的程序员,十分优秀!