Repository navigation
Description
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.
Activity
OneBlue commentedon Jul 22, 2022
Seeky (@Seeky90): by design the WSL2 VM stops if inactive after 60 seconds.
You can change this behavior by setting:
In %userprofile%/.wslconfig. (-1 means that the VM never times out).
NotTheDr01ds commentedon Jul 22, 2022
Blue (@OneBlue) Just to be clear, according to the issue above, the instance shouldn't be inactive. If
cronis 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 underinit. 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.
Seeky90 commentedon Jul 25, 2022
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-TestPlace 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 startOR
Add the following content for no shutdown:
service udev start service ssh startAdd 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
OneBlue commentedon Jul 25, 2022
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 ?Seeky90 commentedon Jul 26, 2022
Hey Blue (@OneBlue)
In case I have the following in my init file (Run from Task Scheduler)
then ps will looks like this when started using a scheduled task:

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

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:
then the result looks as expected:

Seeky90 commentedon Jul 27, 2022
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
hammadrauf commentedon Oct 4, 2022
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..."
--
Seeky90 commentedon Oct 4, 2022
hammadrauf Your test case is different from mine.
As mentioned above it gets caused by a special combination of commands:
This will cause it:
This is totally fine:
hammadrauf commentedon Oct 4, 2022
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.