WSL 从 Windows 运行 linux 而不产生 cmd-window [英] WSL run linux from windows without spawning a cmd-window

查看:19
本文介绍了WSL 从 Windows 运行 linux 而不产生 cmd-window的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 cmd 中运行了 WSL bash.我不将它用于任何用途,它只是挂在那里以保持 WSL 系统的活力.

I have WSL bash running in a cmd. I don't use it for anything, it just hangs there to keep the WSL system alive.

当我启动 X 应用程序时:

When I start X applications:

bash -c "DISPLAY=:0 xmessage hello &"

我得到了这个结果:

我可以毫无问题地关闭命令窗口,但这很烦人.

I can close down the command window without any problems, but it's rather annoying.

如何运行命令而不每次都得到这个cmd窗口?

How can run commands without getting this cmd window every time?

推荐答案

这里有一个更简单的解决方案,但是需要一个基于WSH的辅助脚本runHidden.vbs(见底部):

Here's a simpler solution, which, however, requires a WSH-based helper script, runHidden.vbs (see bottom section):

wscript .
unHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world'"

应用@davv 自己的后台启动技术来避免每次都创建一个新的 bash 实例:

To apply @davv's own launch-in-background technique to avoid creating a new bash instance every time:

一次性操作(例如,在启动时):启动一个隐藏的、保持打开的 bash 窗口.这会产生 2 bash 进程:拥有控制台窗口的 Windows bash.exe 进程和 WSL bash 进程(由 WSL init 单例拥有),然后可用于服务后台命令.

One-time action (e.g., at boot time): launch a hidden, stay-open bash window. This spawns 2 bash processes: the Windows bash.exe process that owns the console window, and the WSL bash process (owned by the WSL init singleton), which is then available for servicing background commands.

wscript .
unHidden.vbs bash # hidden helper instance for servicing background commands

对于每个 X Window 启动命令:用 & 终止每个命令,让它由隐藏的 WSL 运行bash 实例异步,不保持调用 bash 实例的活动:

For every X Window-launching command: Terminate each command with & to have it be run by the hidden WSL bash instance asynchronously, without keeping the invoking bash instance alive:

wscript .
unHidden.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.
  args = args & sep & """" & WScript.Arguments(i) & """"
  sep = " "
next

' Execute the command with its window *hidden* (0)
WScript.CreateObject("Shell.Application").ShellExecute exe, args, "", "open", 0

即使从 GUI 应用程序启动(例如通过 Win+R 调用的 Run 对话框),也不会显示控制台窗口.

Even when launched from a GUI app (such as via the Run dialog invoked with Win+R), this will not show a console window.

如果您的系统配置为默认使用 wscript.exe 执行 .vbs 脚本 (wscript//h:wscript/s,我认为这是默认配置),你可以直接调用runHidden.vbs,如果你把它放在你的%PATH%,仅按文件名(root):runHidden ....

If your system is configured to execute .vbs scripts with wscript.exe by default (wscript //h:wscript /s, which, I think, is the default configuration), you can invoke runHidden.vbs directly, and if you put it in your %PATH%, by filename (root) only: runHidden ....

请注意,脚本的使用不仅限于控制台应用程序:甚至 GUI 应用程序也可以使用它隐藏运行.

Note that use of the script is not limited to console applications: even GUI applications can be run hidden with it.

这篇关于WSL 从 Windows 运行 linux 而不产生 cmd-window的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆