WSL launches linux from windows without cmd window

My WSL bash works in cmd. I do not use it for anything, it just hangs there to support the WSL system.

When I launch X applications:

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

I get this result:

enter image description here

I can close the command window without any problems, but this is pretty annoying.

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

+10
windows bash ubuntu windows-subsystem-for-linux
source share
4 answers

Here is a simpler solution that requires a helper WSH -based script :

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

To apply your own @davv startup methodology in the background, to avoid creating a new bash instance each time:

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

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

For each X Window start command : End each command with & so that it executes as a hidden instance of WSL bash asynchronously, without saving the calling instance of bash :

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

runHidden.vbs source 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 

Even when starting from an application with a graphical interface (for example, through the " Run " dialog box called with Win + R ), the console does not appear in this window.

If you configured your system to run .vbs scripts with wscript.exe by default ( wscript//h:wscript/s ), you can directly call runHidden.vbs and, if you put it in your %PATH% , only the file name (root): runHidden...

Please note that the use of the script is not limited to console applications: even GUI applications can be hidden.

+15
source share

There is another simple solution that requires an external executable. It has no dependencies and was recommended aseering on GitHub .

you can launch bash via run.exe: run.exe bash.exe -c " ". (run.exe is available here: http://www.straightrunning.com/projectrun/ , make sure you download the 64-bit version, the 32-bit version will not be able to find or run bash).

With run in search PATH you can just call

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

+2
source share

So, I just made this workaround. I really hope there is a better way than this, but here it goes:

On the command line, which only lives in order to keep the WSL alive, I have this script run:

wsl_run_server

 #!/bin/bash set -e nc -kl 127.0.0.1 15150 | sh 

And then I have this command to execute commands in the background:

wsl_run_background_command

 if ! pidof -x bin/wsl_run_server; then echo wsl_run_server isnt running! exit 1 fi echo \( $@ \) \& | nc localhost 15150 

from the windows that I then call:

 bash -c "DISPLAY=:0 ~/bin/wsl_run_command xmessage hello" 
+1
source share

What about start / b bash -c "DISPLAY =: 0 xmessage hello"? Did I miss something?

0
source share

All Articles