![](/img/trans.png)
[英]How to send commands from a BAT file to running NodeJS process in windows?
[英]Nodejs - Hide window from spawned bat file process | Windows
我有一个蝙蝠文件,它只是:
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。
但是我不能使用它,因为我实际上是试图用一堆命令来运行一个更复杂的蝙蝠文件。
不幸的是,该文档说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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.