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

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

问题描述

我有在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?

推荐答案

这是一个简单的解决方案,需要

Here's a simpler solution that requires a WSH-based helper script, however:

wscript .\runHidden.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 .\runHidden.vbs bash # hidden helper instance for servicing background commands

对于每个X窗口启动命令 :使用&终止每个命令,以使其由隐藏的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 .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world' &"


runHidden.vbs源代码:


runHidden.vbs source code:

' 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(wscript //h:wscript /s)执行.vbs脚本,则可以直接调用runHidden.vbs,如果将其按文件名放在%PATH%中, root):runHidden ....

If you've configured your system to execute .vbs scripts with wscript.exe by default (wscript //h:wscript /s), 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窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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