When you want to open a Linux GUI app from Windows Command Prompt or PowerShell, you can use the executable that is assigned to your Linux distro in WSL. Such executables are referred to as 'app execution aliases' in Windows Settings and they are found in x410.exe
and you can use it for directly starting X410 in batch files, Windows Command Prompt, PowerShell etc. without providing its full file path.
You can also use wsl
or wsl.exe
command as described in the following post for executing Linux binaries.
Working across Windows and Linux file systems @ Microsoft
You must provide a DISPLAY environment variable that points to X410 if you want to open your Linux GUI apps or desktop in X410. A simple and reusable method to do this would be using a ~/.profile
script and opening Bash as a 'login' shell as described in the following post.
If you're using Ubuntu 20.04 in WSL2, you can execute the following command in Windows Command Prompt to see if your DISPLAY environment variable is set properly.
ubuntu2004.exe run 'echo $DISPLAY'
After executing the above command, you'll only get a blank line even if you added your DISPLAY environment variable to ~/.profile
. While executing the above echo
command, Bash is not invoked as a 'login' shell; hence ~/.profile
is not read.
In order to reuse your ~/.profile
, you should explicitly start Bash as a login shell and execute your command via its -c
switch.
ubuntu2004.exe run "bash --login -c 'echo $DISPLAY'"
Please note that if you're executing the above command in PowerShell, you need to escape the $
character with a `
(backward apostrophe / grave accent) otherwise it'll be interpreted by PowerShell as its own variable.
PS C:\> ubuntu2004.exe run "bash --login -c 'echo `$DISPLAY'"
All in all, if you want to start a Linux GUI app in Ubuntu 20.04 from Windows Command Prompt, for example xclock
, you can execute a command similar to the following. If you're starting a Linux GUI desktop, you can just replace the xclock
command with the one that launches your desktop.
ubuntu2004.exe run "bash --login -c 'xclock -update 1'"
When you execute the above command, ubuntu2004.exe
waits until you terminate xclock
. If you don't want such behavior, you can make it a background process by appending an &
. However, when you push it to the background and Bash exits, all its background processes are also terminated. So, if you want to have your Linux GUI apps keep running even after their parent Bash shell is terminated, you should detach them from their Bash shell. You can use disown
or nohup
for such purpose, but nohup
seems to work better when directly starting an app via Bash as a command-line argument.
ubuntu2004.exe run "bash --login -c 'nohup xclock -update 1 >/dev/null 2>&1 & sleep 1'"
nohup
creates a nohup.out
file for logging output from the executable and >/dev/null 2>&1
in the above example prevents creating such file. The last sleep 1
is not needed in a perfect world... But unfortunately, WSL2 seems to be having a problem executing a background process if Bash terminates too early; you may have to increase the sleep time, or such workaround is not needed at all for your system.
Instead of using the app execution alias for Ubuntu 20.04 as shown above, you can also use wsl.exe
command as shown below to achieve the same result.
wsl.exe -d Ubuntu-20.04 bash --login -c "nohup xclock -update 1 >/dev/null 2>&1 & sleep 1"
The following is an example of Windows batch files that starts xclock
in WSL using the app execution alias method shown above. If you're starting a Linux GUI desktop, you should change the /wm
switch to /desktop
for X410; /desktop
opens X410 in its Desktop mode.
@echo off
REM ### Start X410 in Windowed Apps Mode. If X410 is already running in Desktop Mode,
REM ### it'll be terminated first without any warning message box.
start /B x410.exe /wm
REM ### Start xclock
ubuntu2004.exe run "bash --login -c 'nohup xclock -update 1 >/dev/null 2>&1 & sleep 1'"
The following post has more information about turning a Windows batch file into a shortcut for launching Linux GUI apps.
Pin a Linux GUI app to Start or Taskbar