The WSL instance still shuts down automatically when all WSL terminals are closed, after waiting for a while.
Windows Version
Microsoft Windows [版本 10.0.22621.1555]
WSL Version
WSL 版本: 1.2.0.0 内核版本: 5.15.90.1 WSLg 版本: 1.0.51 MSRDC 版本: 1.2.3770 Direct3D 版本: 1.608.2-61064218 DXCore 版本: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows 版本: 10.0.22621.1555
Are you using WSL 1 or WSL 2?
- [X] WSL 2
- [ ] WSL 1
Kernel Version
默认分发: rocky9 默认版本: 2
Distro Version
No response
Other Software
docker --version
# Docker version 20.10.21, build baeda1f
Repro Steps
- Execute "wsl" in cmd to launch wsl.
- Press Ctrl + D to exit the terminal of the wsl instance.
- Wait for a while and then execute "wsl -l -v". Check if the wsl status is "Stopped".
I have already configured to prevent automatic shutdown, the configuration is as follows:
.wslconfig
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=6
# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
# kernel=C:\\temp\\myCustomKernel
# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=1GB
safeMode=false
# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=true
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true
guiApplications=true
# Disables nested virtualization
nestedVirtualization=true
# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true
vmIdleTimeout=604800000
[root@DESKTOP-2U5HSUB ~]# cat /etc/wsl.conf
[wsl]
autostop=false
[boot]
systemd = true
# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
command = ls -al
# Automatically mount Windows drive when the distribution is launched
[automount]
# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true
# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c.
# root = /
# DrvFs-specific options can be specified.
#options = "metadata,uid=0,gid=0,umask=022,fmask=11"
mountFsTab = true
# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
# hostname = DemoHost
generateHosts = true
generateResolvConf = true
# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = true
appendWindowsPath = true
# Set the user when launching a distribution with WSL.
[user]
default = root
Expected Behavior
The WSL instance keeps running even after closing all terminals.
Actual Behavior
The WSL instance automatically shuts down after waiting for a while after closing all terminals.
Diagnostic Logs
DebugConsole logs the following when WSL is closed.
[ 146.602703] Exception:
[ 146.602709] Operation canceled @p9io.cpp:258 (AcceptAsync)
[ 146.604567]
?????????????
This is intended, see https://github.com/microsoft/WSL/issues/8654#issuecomment-1195973431.
The vmIdleTimeout isn't what it seems to be.
There is another thread for this topic https://github.com/microsoft/WSL/issues/9667#issuecomment-1440651201 which mentioned an hidden setting but that isn't revealed (yet).
@ImmortalD
If you setup systemd in WSL 2 with https://github.com/DamionGans/ubuntu-wsl2-systemd-script , WSL 2 won't stop if you close all the shells/terminals.
Please note that that repository is not updated any more, but it still works.
By default it sets systemd to use basic.target, but you can change it in the two files enter-systemd-namespace and start-systemd-namespace (one change in each file).
For WSL 2 distro Ubuntu-22.04 there's also a need to patch enter-systemd-namespace with the patch from https://github.com/DamionGans/ubuntu-wsl2-systemd-script/issues/36#issuecomment-732090101 (the change goes to the end of enter-systemd-namespace).
Just provide a workaround. If you allow WSL2 distro able to execute Windows program, you can try this workaround https://github.com/microsoft/WSL/issues/8854#issuecomment-1490454734.
@ImmortalD
If you setup systemd in WSL 2 with https://github.com/DamionGans/ubuntu-wsl2-systemd-script , WSL 2 won't stop if you close all the shells/terminals.
Please note that that repository is not updated any more, but it still works.
By default it sets systemd to use basic.target, but you can change it in the two files enter-systemd-namespace and start-systemd-namespace (one change in each file).
For WSL 2 distro Ubuntu-22.04 there's also a need to patch enter-systemd-namespace with the patch from DamionGans/ubuntu-wsl2-systemd-script#36 (comment) (the change goes to the end of enter-systemd-namespace).
Thank you for your reply. I will try it.
Just provide a workaround. If you allow WSL2 distro able to execute Windows program, you can try this workaround #8854 (comment).
Thanks for your reply. My Docker has started in the WSL, and I used Docker to start a database, but it still stops.
[wsl]
autostop=false
Is that a ChatGPT hallucination? I don't see that as a valid configuration option in the doc or the recent release notes.
I'll propose two easy solutions:
-
Use Docker Desktop rather than installing Docker Engine manually. Docker Desktop contains a keepalive process to prevent WSL2 from stopping.
-
My personal solution is to install
keychainand add it to your startup config (e.g.~/.bashrc). Advantages:- Lightweight
- Long running process that will prevent termination
- Singleton design that means it will only run one copy, no matter how many times you start a shell
- In the default repositories of most all distributions. E.g.
sudo apt install keychain) - You may find it useful apart from just keeping WSL2 from stopping ;-)
Found a fix for this problem: https://github.com/microsoft/WSL/issues/10138#issuecomment-1593856698
Found a fix for this problem: #10138 (comment)
thanks you for you reply, but i exec the command has some errors
wsl --exec dbus-launch true
erros:
<3>WSL (666) ERROR: CreateProcessEntryCommon:577: execvpe dbus-launch failed 2
<3>WSL (666) ERROR: CreateProcessEntryCommon:586: Create process not expected to return
@ImmortalD Ok could be a difference in the WSL 2 version. I installed 1.2.5 yesterday.
@ImmortalD Ok could be a difference in the WSL 2 version. I installed 1.2.5 yesterday.
My Windows automatically updates itself. We have the same version.
wsl -v
WSL 版本: 1.2.5.0
内核版本: 5.15.90.1
WSLg 版本: 1.0.51
MSRDC 版本: 1.2.3770
Direct3D 版本: 1.608.2-61064218
DXCore 版本: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows 版本: 10.0.22621.1848
@ImmortalD How about the distro version? I use Ubuntu-22.04
I encountered the same problem and it was resolved by turning off systemd
[boot]
systemd=false
Try if this fixes it: https://github.com/microsoft/WSL/issues/10138#issuecomment-1590076363
These are all the apt-get commands I've run in the WSL 2 Ubuntu-22.04 instance
apt-get update
apt-get dist-upgrade
apt-get install apache2
apt-get install mesa-utils foot glmark2 glmark2-wayland
apt-get update
apt-get dist-upgrade
apt-get install btop glances nmon iftop iptraf-ng
apt-get install mate-terminal
apt-get install xterm
Edit: for some reason I need to use "dbus-launch mate-terminal" to start it
Edit 2: there's some weird bug with something to do with wayland and mate-terminal. If I just launch it with "dbus-launch mate-terminal", when switching between another Windows program and mate-terminal makes mate-terminal shrink one column at a time.
To launch it in X11 mode, use "dbus-launch mate-terminal --display=:0" and the problem doesn't manifest itself any more.
I solved in this way: wsl --setdefault Ubuntu-22.04
then I put this command inside bat file on startup folder located in: C:\Users<username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
@echo off wsl --exec dbus-launch true
and it always be running at every reboot. Many thanks to all of you
By customizing the script you can keep WSL2 running in the background, if there is a more elegant way, click me
First, create a script file keep_wsl_running
#!/bin/bash
while true;
do
sleep 10;
done;
Then run the script when .bash_profile or .zshrc starts
# keep wsl running background
ps -ef | grep keep_wsl_running | grep -v grep >> /dev/null
if [ $? -ne 0 ]; then
nohup ~/Private/bin/keep_wsl_running >> /dev/null 2>&1 &
fi
Running the nginx container with docker in wsl, closing all Terminal windows, and being able to access nginx through a browser no matter how long it takes, this one met my expectations
@srikker Start the WSL 2 instance like this, and it'll not shutdown automatically:
wsl.exe --exec dbus-launch true & bash
@srikker Start the WSL 2 instance like this, and it'll not shutdown automatically:
wsl.exe --exec dbus-launch true & bash
This method requires manual startup at the command line, which increases the operation cost. I set Ubuntu as the default configuration of WinTerminal, open is not able to be implemented in this way in the boot command. in the end I used a custom script, only configure once.
@srikker You can create a new Windows Terminal profile and define this batch file as its "Command line":
@echo off
wsl.exe --exec dbus-launch true & bash
I encountered the same problem and it was resolved by turning off systemd
[boot] systemd=false
Works just fine for me with systemd enabled, kinda the whole reason I want WSL to persist is so I can enable startup services (ssh) with systemd from windows land.
@hemenge133 If you start up the WSL 2 instance like this, it will continue running even if you close all the shells/terminals:
wsl --distribution Ubuntu-22.04 --exec dbus-launch true & wsl --distribution Ubuntu-22.04 --cd ~
any official update for this?
@berryscone What do you expect Microsoft to do? They've designed WSL 2 so that the instance will automatically shutdown after you've closed the shells that keep it running.
@aki-k I expect for them to provide an option to disable this behavior. Are you a person in charge of WSL in MS?
@berryscone No, I don't work for Microsoft. Now that Microsoft and Canonical created WSL, they should make it work as well as they can without artificial limitations.
Here's the command you can use so that the WSL 2 instance doesn't shutdown automatically:
wsl --distribution Ubuntu-22.04 --exec dbus-launch true & wsl --distribution Ubuntu-22.04 --cd ~
I stumbled accross this and related issues being convinced, that this wasn't an issue for me over the past year(s).
After trying to follow the suggestions dbus-launch true indeed worked for me however I did not find a nice way to run this truely in backgroud. (TaskScheduler, AutoStart, ...) Therefore I continued to investigate what was different before I faced this issue.
In the beginning my explorer didn't show the Linux location and some older programs I was using did not work with UNC paths. There I connected mit default wsl instance root's UNC as a network drive and set it to reconnect automatically.
After removing this - since no longer needed because the mentioned reasons did no longer apply - the problems with shutting down of my instances started. I now removed any annoying injection of dbus-launch true and reverted my setup to having the connected network drive.
And it works again like a charm. Shortly after windows reconnects, my instance boots up and all systemd services are active in the background.
I post this just in case it might help some of you who are not satisfied with the dbus-launch solution.
@parapilot I wrote about the automatic WSL 2 start at system startup for both Windows service method and for Task Scheduler method here:
https://github.com/microsoft/WSL/issues/9231#issuecomment-1715312138 https://github.com/microsoft/WSL/issues/9231#issuecomment-1718985779
How long do developers need to endure this type of nonsense?
@imsir0vic The fix for the WSL 2 instance auto-shutdown is to start it like this:
wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true & wsl.exe --distribution Ubuntu-22.04
@imsir0vic The fix for the WSL 2 instance auto-shutdown is to start it like this:
wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true & wsl.exe --distribution Ubuntu-22.04
Have you even tested this??
PS C:\Users\pedro> wsl -d Debian
nostromo% which dbus-launch
dbus-launch not found
nostromo%