WSL: WSL cannot be run by scheduled task

Windows Version

Microsoft Windows [Version 10.0.20348.2031]

WSL Version

WSL-Version: 1.2.5.0

Are you using WSL 1 or WSL 2?

  • WSL 2
  • WSL 1

Kernel Version

Linux version 5.15.90.1-microsoft-standard-WSL2 (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220) # 1 SMP Fri Jan 27 02:56:13 UTC 2023

Distro Version

Ubuntu 22.04

Other Software

No response

Repro Steps

On Windows Server 2022 open the Task Scheduler and create a new task.

In the Properties of this task, under Triggers, set: image

Actions tab, create an action like: image

Tested with this configuration: image

When launched, the task will exit with 0x1 status (see details below) and running:

wsl -l --running

we don’t get Ubuntu WSL.

I am aware of https://github.com/microsoft/WSL/issues/8835 where it is shown a workaround but it seems to not work in order to start WSL.

The user used for this task belongs to Administrators group that is tied with Log on as a batch job.

Expected Behavior

Scheduled task ended by 0x0 code status.

Actual Behavior

Scheduled task ended by 0x1 code status and it failed in “Action completed” task category with the following message:

Task Scheduler successfully completed task "\Start task-name" , instance "{be3a337a-71ea-4580-87bd-10b162076149}" , action "C:\Windows\System32\wsl.exe" with return code 2147942401.

Diagnostic Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (1 by maintainers)

Most upvoted comments

/dupe 9231

@D3vil0p3r Please follow these steps to run a WSL 2 instance at system startup through the Task Scheduler service:

https://github.com/microsoft/WSL/issues/9231#issuecomment-1718985779

You need to observe these restrictions to be able to do that:

  1. remove local admin rights from the user (Windows_username) that is running the new Task Scheduler task

  2. add “Log on as a batch job” right for the user that is setup for the Task Scheduler task (Windows_username) in secpol.msc Security Settings/Local Policies/User Rights Assignment/Log on as a batch job

  3. you can not run the wsl.exe command once logged into Windows with the same username (Windows_username) that you setup in the Task Scheduler task. If you do, the WSL 2 instance stops. Even a “wsl -l -v” command to list the WSL 2 instances will stop the WSL 2 instance

  4. don’t use Microsoft systemd implementation (/etc/wsl.conf: systemd=true) but use Damion Gans’ systemd implementation https://github.com/DamionGans/ubuntu-wsl2-systemd-script

    that repository is set to read-only mode for some reason, but it still works after these changes:

    change basic.target to multi-user.target in both start-systemd-namespace and enter-systemd-namespace

    change enter-systemd-namespace according to this comment’s shell script fragment

    https://github.com/DamionGans/ubuntu-wsl2-systemd-script/issues/36#issuecomment-732090101

    run “bash ubuntu-wsl2-systemd-script.sh” to enable it for the next startup