Skip to content

WSL2 does not keep running since Version 0.60.0 #8642

Closed

Description

@Seeky90

Version

Version 10.0.22000.739

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

5.10.102.1

Distro Version

Ubuntu-20.04

Other Software

WSL Version 60.0 and higher

Repro Steps

To start some services I created a script and added it to the event scheduler. It starts sshd and cron.
After the start of the system, if I am fast (2-3 minutes) I can observe that the script has been run.

After waiting a bit WSL has been shut down and is restarted when I open the terminal. Then the services do not run.

For simplicity follow these repro steps:
Create a task scheduler task:

<?xml version="1.0" encoding="UTF-16"?> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Date>2022-02-17T07:48:10.6191908</Date> <Author>username</Author> <URI>\test</URI> </RegistrationInfo> <Triggers /> <Principals> <Principal id="Author"> <UserId>S-1-5-21-2139355670-3375578443-2995265674-8217</UserId> <LogonType>InteractiveToken</LogonType> <RunLevel>HighestAvailable</RunLevel> </Principal> </Principals> <Settings> <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> <AllowHardTerminate>true</AllowHardTerminate> <StartWhenAvailable>false</StartWhenAvailable> <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> <IdleSettings> <StopOnIdleEnd>true</StopOnIdleEnd> <RestartOnIdle>false</RestartOnIdle> </IdleSettings> <AllowStartOnDemand>true</AllowStartOnDemand> <Enabled>true</Enabled> <Hidden>false</Hidden> <RunOnlyIfIdle>false</RunOnlyIfIdle> <WakeToRun>false</WakeToRun> <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> <Priority>7</Priority> </Settings> <Actions Context="Author"> <Exec> <Command>C:\Windows\System32\wsl.exe</Command> <Arguments>--shutdown</Arguments> </Exec> <Exec> <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command> <Arguments>-windowstyle hidden -ExecutionPolicy Bypass C:\Users\username\test.ps1</Arguments> </Exec> </Actions> </Task>

Add into the Powershell script C:\Users\username\test.ps1:
bash.exe -c "sudo /opt/wsl-init.sh

Into /opt/wsl-init.sh add:
service cron start

Expected Behavior

Since the service is running all the time I expect the WSL session to stay active.

Actual Behavior

Since version 60 of WSL this does not work anymore. After a few minutes wsl --list --verbose shows that the WSL instance has been stopped.

Diagnostic Logs

#8611
WslLogs-2022-07-22_07-29-04.zip

Activity

  1. OneBlue commented on Jul 22, 2022

    @OneBlue
    Collaborator

    Seeky (@Seeky90): by design the WSL2 VM stops if inactive after 60 seconds.

    You can change this behavior by setting:

    [wsl2]
    vmIdleTimeout=-1
    

    In %userprofile%/.wslconfig. (-1 means that the VM never times out).

  2. NotTheDr01ds commented on Jul 22, 2022

    @NotTheDr01ds

    Blue (@OneBlue) Just to be clear, according to the issue above, the instance shouldn't be inactive. If cron is running, which the issue states it is, then the instance shouldn't self-terminate. The instance should only self-terminate when there are no processes running under init. That's been the design since the early WSL1 days, right?

    Seeky (@Seeky90) Does this only happen when run via a Scheduled Task? If so, probably better to list out the task settings in a bulleted list that's more "human readable" than the XML ;-).

    Also, you might want to ask this over on Ask Ubuntu or Super User. I suspect something in your script rather than a change in WSL behavior, but I'd be happy to try to reproduce if you can make the steps more readable. I haven't tried a scheduled task myself with the "Preview", personally.

    I do see you have a custom kernel running which you might want to try disabling as a normal troubleshooting step.

  3. Seeky90 commented on Jul 25, 2022

    @Seeky90
    Author

    Thanks for the clarification Douglas (@NotTheDr01ds) . Thats just as it is. Blue (@OneBlue) You might be correct about the timeout. For some reason the started tasks do not run under init, please see below.

    I have done some tests and am quite sure that a bug leads to this behavior. I am not sure if it is due to how systemd behaves or how to wsl behaves.

    Please try to reproduce it yourself:

    Create a Schedules Task, I have added a file wich you can import with powershell using:
    Register-ScheduledTask -xml (Get-Content -Raw WSL-Test.xml) -TaskName WSL-Test

    Place the file wsl-test.ps1 in your user directory.

    Create a file /opt/wsl-test.sh
    Make it executable.

    Add the following content for auto shutdown:
    service udev start & service ssh start

    OR

    Add the following content for no shutdown:
    service udev start service ssh start

    Add the /opt/wsl-test.sh to /etc/sudoers as NOPASSWD

    When I start the task from the task scheduling by hand I can see that in case I use the & on one service, both services are not run under init anymore, which leads to the auto shutdown. When the & is omitted both services run under init. I'd expect only the one with & to run in background, not both.
    When I start the task from the terminal both processed are run unter init, no matter of the &.

    wsl-test.zip

  4. OneBlue commented on Jul 25, 2022

    @OneBlue
    Collaborator

    Seeky (@Seeky90): What exactly do you mean by the the started tasks do not run under init ? Can you share a screenshot of that htop looks like in this scenario ?

  5. Seeky90 commented on Jul 26, 2022

    @Seeky90
    Author

    Hey Blue (@OneBlue)

    In case I have the following in my init file (Run from Task Scheduler)

    #!/bin/bash

    service ssh start
    service udev start &
    service cron start
    service atd start
    /opt/setIP.sh
    echo $(date) >> /tmp/wsl-init-date

    then ps will looks like this when started using a scheduled task:
    ps-fail

    Shutting down the WSL, starting it by hand and running the same script with sudo gives me this:
    ps-okay-manualrun

    After I created this ticket with your hint about the timeout (Which did not work for me, will create a ticket for this)
    I changed the script to the following:

    #!/bin/bash

    service ssh start
    service cron start
    service atd start
    /opt/setIP.sh
    echo $(date) >> /tmp/wsl-init-date
    nohup service udev start

    then the result looks as expected:
    ps-okay

  6. Seeky90 commented on Jul 27, 2022

    @Seeky90
    Author

    Seeky (@Seeky90): by design the WSL2 VM stops if inactive after 60 seconds.

    You can change this behavior by setting:

    [wsl2]
    vmIdleTimeout=-1
    

    In %userprofile%/.wslconfig. (-1 means that the VM never times out).

    As Douglas (@NotTheDr01ds) explained here, this is not a solution to the issue that the VM turns off even though there is still something running: #8659

  7. hammadrauf commented on Oct 4, 2022

    @hammadrauf

    I am able to keep WSL2 running in background on Windows 11, using the following Power Shell 7 script. Also tried it from Task Schedular, works as expected.
    The script is getting the IP address of the WSL instance, Then it runs a never ending background process. It then Forwards a few ports to the Host computer using the IP address from previous step. For Port forwarding (netsh) "IP Helper" service should be running.

    Script is executed like: "C:\Program Files\PowerShell\7\pwsh.exe" -ExecutionPolicy Bypass -File "C:\scripts\wsl2\start-wsl-forward-ports.ps1"

    Script file is given below:

    $wsl_ip = (wsl -d "Ubuntu-22.04" --shell-type login hostname -I).split(" ")[0]
    Write-Host "WSL Machine IP: ""$wsl_ip"""
    wsl -d "Ubuntu-22.04" --shell-type login tail -n 1 -f /dev/null &
    netsh interface portproxy add v4tov4 listenport=4505 connectport=32377 connectaddress=$wsl_ip
    netsh interface portproxy add v4tov4 listenport=4506 connectport=32378 connectaddress=$wsl_ip
    netsh interface portproxy add v4tov4 listenport=8092 connectport=32380 connectaddress=$wsl_ip
    Write-Host "Completed - Start WSL and port forwarding..."

    --

  8. Seeky90 commented on Oct 4, 2022

    @Seeky90
    Author

    hammadrauf Your test case is different from mine.

    As mentioned above it gets caused by a special combination of commands:

    This will cause it:

        #!/bin/bash
    
        service ssh start
        service udev start &
        service cron start
        service atd start
    

    This is totally fine:

        #!/bin/bash
    
        service ssh start
        service cron start
        service atd start
        nohup service udev start
    
  9. hammadrauf commented on Oct 4, 2022

    @hammadrauf

    Maybe the last command in script should be the background job...... I had issues also till I made the last command in WSL to be the background job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions