WSL: Unable to start WSL via Task Scheduler
Version
22000.978
WSL Version
- WSL 2
- WSL 1
Kernel Version
5.15.57.1
Distro Version
Ubuntu 22.04
Other Software
No response
Repro Steps
Create a powershell script with the following:
# start SSH server and other applications in Ubuntu
wsl -d Ubuntu-22.04 -u root service ssh start
Start-Sleep -s 5
# add port forwarding rule (from WSL to host) for port 4242
$wsl_ip = (wsl hostname -I).trim()
Write-Host "WSL Machine IP: ""$wsl_ip"""
netsh interface portproxy add v4tov4 listenport=4242 connectport=4242 connectaddress=$wsl_ip
Create a Task Scheduler task to run the powershell script on boot.
- Run whether use is logged in or not
- Run with highest privileges
- Trigger: at startup (30 second delay)
- Action: start a program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - Action: Add arguments:
-ExecutionPolicy Bypass -File C:\Scripts\startwsl.ps1 - Settings: Allow task to be run on demand
Restart the machine, or manually run the task.
Expected Behavior
Upon reboot or manually running the task, the WSL instance should start and the ssh daemon should be running. This worked in previous versions of WSL (on Windows 10).
Actual Behavior
Either on reboot or on manually executing the task, WSL does not start.
C:\Windows\system32>wsl --list --running
There are no running distributions.
If I manually run the following command via the command prompt, WSL and the ssh daemon start properly.
C:\Windows\system32>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\startwsl.ps1
* Starting OpenBSD Secure Shell server sshd [ OK ]
Starting seafile daemon ...
Started: seafile daemon ...
WSL Machine IP: "172.21.82.221"
C:\Windows\system32>wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu-22.04 (Default)
In summary, running the same powershell script to launch WSL works from the command prompt but does not work from the task scheduler.
Diagnostic Logs
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 19
- Comments: 55 (1 by maintainers)
For anyone who ends up finding this as the Google top search hit:
There is a fix in the WSL September 2023 update.
Steps to solve the issue:
wsl --update --pre-release"C:\Program Files\WSL\wsl.exe". IMPORTANT: There are multiple locations for the WSL executable with this update - the one under Program Files is the only one that will work. ¯\_(ツ)_/¯Confirmed working for me on Win10 and WSL 2.0.1.
The whole point of this ticket is to be able to run WSL2 in session 0, i.e. like a service without a console or GUI. This implies that the task scheduler option Run whether user is logged in or not is checked, as stated in the issue.
As a minimal repo, run the following commands in PowerShell:
When you then look at the task status, it will indicate failure (error code 1):
Just confirmed that using the current WSL-Version: 1.2.0.0
I encountered the same issue. It was resolved by
I think I have found a solution. According to this StackOverflow answer, we can escalate a process out of session 0 using psexec.exe.
C:\<your psexec install path>\PsExec.exe -i 1 <your original command>I have the same problem. This problem seems to be specific to the Microsoft Store version of wsl. If I uninstall the Microsoft Store WSL app, task schedule can properly start wsl. I tried the vbs trick mentioned here without success. This problem seems to be related to the “Unable to start wsl.exe from a Session 0 prompt which users can run into when trying to start WSL from an ssh session” limitation mentioned in the release blogpost.
Thanks @astuomi.
I tried this several times but whenever I manually ran the task like this:
The status would change to “Running” but it didn’t seem to be doing anything at all.
It wasn’t until I went back into Properties and used the “Browse” button to pick
wsl.exethat it actually worked:Copy-and-pasting
"C:\Program Files\WSL\wsl.exe"does not work, despite the path being exactly the same.Actually, Task Scheduler doesn’t show the job as finished. My script seems to have finished because I can see the last line has finished executing, but Task Scheduler thinks it’s still running. So it’s still a bit sketchy but… we’ll see if it works.
Edit: Seems to be working.
The only thing I found that worked is to change the user on the task to the group “Users”. The downside is that it opens a terminal on the screen. I can’t seem to minimize it on execution for some reason. Strange how everyone has a different method that works.
I can confirm that it works if you run the script with “only when user is logged on” and triggered at log on.
@brettus78 I have created a GitHub repo for my solution of running wsl at startup. I have also installed the Windows 11 22H2 update so I think it shouldn’t be the issue.
/dupe #9231
@aki-k Thank you for your suggestion, but it does not work.
Using
cmd /kleaves a dangling cmd.exe in session 0 behind, which may have tricked you into reasoning that wsl.exe is running (which it is not).BTW: /dupe https://github.com/microsoft/WSL/issues/9231
I can confirm, as @cheng-chi said, that this issue is only apparent in the Microsoft Store version of WSL. I am able to start WSL via the Task Scheduler when I use the non-Microsoft Store version.