Nodejs-从生成的bat文件进程隐藏窗口|视窗

时间:2018-07-14 12:59:17

标签: node.js windows cmd child-process spawn

我有一个蝙蝠文件,只是:

ping localhost -t

当我这样做时:

const cp = require('child_process');
const c = cp.spawn(
  'test.bat',
  [],
  {stdio: ['ignore', log, log], detached: true, shell: false, windowsHide: true}
)

它打开一个新的命令提示符并输出到那里,我希望它不显示任何窗口,并且还希望将输出重定向到指定的fd。

如果我这样做:

const c = cp.spawn(
  'ping', 
  ['localhost', '-t'],
  {stdio: ['ignore', log, log], detached: true}
);

外壳弹出,该命令在后台运行,并记录到指定的fd。

但是我不能使用它,因为我实际上是试图用一堆命令来运行一个更复杂的蝙蝠文件。

1 个答案:

答案 0 :(得分:0)

不幸的是,医生说https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows

  

但是,在Windows上,.bat和.cmd文件在没有终端的情况下无法单独执行

不过,您可以使用这篇帖子https://serverfault.com/questions/9038/run-a-bat-file-in-a-scheduled-task-without-a-window中的提示:

=== test.vbs ===

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "test.bat" & Chr(34), 0
Set WinScriptHost = Nothing

===您的nodejs脚本===

spawn('cmd.exe', ['/c', 'test.vbs'], {
    shell: false,
    detached: true,
    stdio: 'ignore',
    windowsHide: true
}).unref()