6

It is clear that it is quite tricky to get WSL to start at boot time, and that there are a variety of methods that have been used to achieve this goal.

A (Dec 24 2022) answer to a recent post here notes that Microsoft broke it when WSL moved to their store. WSL can no longer run as a service or a scheduled task because it will no longer run in session 0. superuser question on running wsl on windows boot. Then a second answer by @andrus states that one method that does work (as of Dec 25 2022) is to use set object = createobject("wscript.shell") object.run "wsl.exe", 0 still works with latest WSL and Windows 11.

Presumably that code is to be used as a vbs script, and it is to be started from the task scheduler along the lines of this article.

I haven't been able to make it work. I can start wsl using the vbs script from the command line (powershell), but it doesn't work when started from task scheduler. In case it matters, I am running windows 11 pro, downloaded wsl from the microsoft store, installed wsl using "wsl --install Ubuntu" (to install 22.04), then configured wsl to use systemd, then added the relevant "network bridge" changes in both windows, and wsl so sshd starts and the machine gets its own IP#. All of that seems to work really well when started from the command line (using wscript or cscript). But I just can't see any evidence that wsl actually starts when the vbs script is invoked from the task scheduler. Tracing the return codes from task scheduler shows only "0" return codes.

So can anyone confirm that the statement from Andrus that the solution is still working for them when they use a modern version of WSL 2, installed via microsoft store? And perhaps point me to the best description of exactly how to set it up?

wsl --version returns

WSL version: 1.0.3.0
Kernel version: 5.15.79.1
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.1105
Geeky51
  • 131

3 Answers3

4

I'm using this, save as a .vbs. Working on the latest WSL2:

command = "wsl.exe"
 set shell = CreateObject("WScript.Shell")
 shell.Run command,0

One advantage of using vbs is it starts silently with nothing in the taskbar or terminal windows opening.

1

It does not currently appear possible to start WSL as a service. This question is now covered a bit more thoroughly in this superuser question which provides some documentation based upon a discussion with Microsoft WSL teams on github. There were also no comments offered confirming success with modern installations of WSL2.

Geeky51
  • 131
0

Have you tried

wsl --set-version Ubuntu-20.04 2

If you manually installed WSL prior to the wsl --install command being available, you may also need to enable the virtual machine optional component used by WSL 2 and install the kernel package if you haven't already done so.

And then you should be able using something like this (just an example)

' terminator.vbs
myCd = "~"
If WScript.Arguments.Length > 0 Then
    myCd = "'$(wslpath -u '" & WScript.Arguments(0) & "')'"
End If
args = "bash" & " -c ""cd " & myCd & "; DISPLAY=:0 terminator"""
WScript.CreateObject("Shell.Application").ShellExecute "C:\Windows\System32\wsl.exe", args, "", "open", 0
Turdie
  • 163