bash 如何在 Node.js 中打开命令行窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31737743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a command line window in Node.js?
提问by Stephan Ahlf
How to open a new command line window and execute a bash command which runs in a separate independent process?
如何打开一个新的命令行窗口并执行在单独的独立进程中运行的 bash 命令?
I tried
我试过
var child_process = require('child_process');
child_process.execSync("cmd.exe /K node my-new-script.js parm1 parm2);
but it opens no new window and I need an independent process (if possible). The background is I am experimenting with electron and wrote some node command line scripts. Unfortunately within electron environment spawning processes results often in weird behavior and console log output is more than ugly.
但它没有打开新窗口,我需要一个独立的进程(如果可能的话)。背景是我正在试验电子并编写了一些节点命令行脚本。不幸的是,在电子环境中,生成过程通常会导致奇怪的行为,并且控制台日志输出不仅丑陋。
By the way I would need something equivalent to OS X and Linux.
顺便说一句,我需要类似于 OS X 和 Linux 的东西。
回答by brandonscript
For Windows, you'll need to use the start
command:
对于 Windows,您需要使用以下start
命令:
start cmd.exe /K node my-new-script.js parm1 parm2
For OS X, you can use:
对于 OS X,您可以使用:
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
For other *nix distros, you'll need to look those up as each one is slightly different.
对于其他 *nix 发行版,您需要查找它们,因为每个发行版都略有不同。