Perform both these tasks - on Windows and inside WSL2 - to get a fully-automatic setup that "does the right thing" even after reboot without manual intervention:
Create a Scheduled Task to start wsldhost.exe
at login
- Open
Task Scheduler
- Actions →
Create Task...
- General (tab): Check
Run with highest privileges
- Triggers (tab): Click
New
, selectAt log on
underBegin the task
. - Actions (tab): Click,
New
, selectStart a program
underAction
, setProgram/script
to the path ofwsldhost.exe
wherever it is placed, setAdd arguments
to--daemon
. - Conditions (tab): Uncheck
Start the task only if the computer is on AC power
andStop if the computer switches to battery power
- Settings (tab): Uncheck
Stop the task if it runs longer than
It should now start up at every boot as Administrator with the --daemon
option. Now start wsldhost.exe
by right-click-ing on the newly
created task and clicking Run
.
- Make sure
xset
is installed, e.g. withsudo apt-get install x11-xserver-utils
in Debian-based distributions. - Add this to your
~/.profile
or~/.bash_profile
or~/.zlogin
:
export DISPLAY=:0
if ! pgrep wsld >> /dev/null 2>&1 ; then
nohup wsld > /dev/null < /dev/null 2>&1 &
disown
# sleep until $DISPLAY is up
while ! xset q > /dev/null 2>&1 ; do
sleep 0.3
done
fi
Using xset q
to test the $DISPLAY
makes it possible to run a command like wsl.exe bash --login -c some-terminal
, otherwise some-terminal
will fail because the $DISPLAY
isn't ready yet.