这是一个更简单的解决方案,然而,这需要一个WSH https://en.wikipedia.org/wiki/Windows_Script_Host基于辅助脚本, runHidden.vbs
(见底部):
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world'"
应用 @davv 自己的后台启动技术来避免创建新的bash
每次实例:
One-time action(例如,在启动时):启动隐藏的、保持打开的bash
窗户。这会产生2 bash
过程:Windows bash.exe
拥有控制台窗口的进程和 WSLbash
进程(由 WSL 拥有)init
单例),然后可用于服务后台命令.
wscript .\runHidden.vbs bash # hidden helper instance for servicing background commands
对于每个 X Window 启动命令:终止每个命令&
让其由隐藏的 WSL 运行bash
实例异步地,不保留invoking bash
实例存活:
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world' &"
runHidden.vbs
源代码:
' Simple command-line help.
select case WScript.Arguments(0)
case "-?", "/?", "-h", "--help"
WScript.echo "Usage: runHidden executable [...]" & vbNewLine & vbNewLine & "Runs the specified command hidden (without a visible window)."
WScript.Quit(0)
end select
' Separate the arguments into the executable name
' and a single string containing all arguments.
exe = WScript.Arguments(0)
sep = ""
for i = 1 to WScript.Arguments.Count -1
' Enclose arguments in "..." to preserve their original partitioning, if necessary.
if Instr(WScript.Arguments(i), " ") > 0 then
args = args & sep & """" & WScript.Arguments(i) & """"
else
args = args & sep & WScript.Arguments(i)
end if
sep = " "
next
' Execute the command with its window *hidden* (0)
WScript.CreateObject("Shell.Application").ShellExecute exe, args, "", "open", 0
Even when launched from a GUI app (such as via the Run
dialog invoked with Win+R), this will not show a console window.
如果您的系统配置为执行.vbs
脚本与wscript.exe
默认情况下 (wscript //h:wscript /s
,我认为这是默认配置),你可以调用runHidden.vbs
直接,如果你把它放在你的%PATH%
,仅按文件名(根):runHidden ...
.
请注意,脚本的使用不限于console应用程序:甚至 GUI 应用程序也可以用它隐藏运行。