Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The WSL instance still shuts down automatically when all WSL terminals are closed, after waiting for a while. #9968

Open
1 of 2 tasks
ImmortalD opened this issue Apr 14, 2023 · 41 comments

Comments

@ImmortalD
Copy link

ImmortalD commented Apr 14, 2023

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?

  • 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

  1. Execute "wsl" in cmd to launch wsl.
  2. Press Ctrl + D to exit the terminal of the wsl instance.
  3. 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]
?????????????
@MorphBonehunter
Copy link

This is intended, see #8654 (comment).
The vmIdleTimeout isn't what it seems to be.
There is another thread for this topic #9667 (comment) which mentioned an hidden setting but that isn't revealed (yet).

@aki-k
Copy link

aki-k commented Apr 17, 2023

@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).

@firejox
Copy link

firejox commented Apr 17, 2023

Just provide a workaround. If you allow WSL2 distro able to execute Windows program, you can try this workaround #8854 (comment).

@ImmortalD
Copy link
Author

@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.

@ImmortalD
Copy link
Author

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.

@NotTheDr01ds
Copy link

[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 keychain and 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 ;-)

@aki-k
Copy link

aki-k commented Jun 16, 2023

Found a fix for this problem: #10138 (comment)

@ImmortalD
Copy link
Author

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

image

@aki-k
Copy link

aki-k commented Jun 16, 2023

@ImmortalD Ok could be a difference in the WSL 2 version. I installed 1.2.5 yesterday.

@ImmortalD
Copy link
Author

@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

@aki-k
Copy link

aki-k commented Jun 16, 2023

@ImmortalD How about the distro version? I use Ubuntu-22.04

@fcying
Copy link

fcying commented Jun 16, 2023

I encountered the same problem and it was resolved by turning off systemd

[boot]
systemd=false

@aki-k
Copy link

aki-k commented Jun 16, 2023

Try if this fixes it: #10138 (comment)

@aki-k
Copy link

aki-k commented Jun 16, 2023

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.

@Frenz86
Copy link

Frenz86 commented Jun 21, 2023

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

@srikker
Copy link

srikker commented Sep 2, 2023

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

@aki-k
Copy link

aki-k commented Sep 2, 2023

@srikker Start the WSL 2 instance like this, and it'll not shutdown automatically:

wsl.exe --exec dbus-launch true & bash

@srikker
Copy link

srikker commented Sep 2, 2023

@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.

@aki-k
Copy link

aki-k commented Sep 2, 2023

@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

@hemenge133
Copy link

hemenge133 commented Sep 10, 2023

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.

@aki-k
Copy link

aki-k commented Sep 11, 2023

@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 ~

@berryscone
Copy link

any official update for this?

@aki-k
Copy link

aki-k commented Nov 17, 2023

@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.

@berryscone
Copy link

@aki-k I expect for them to provide an option to disable this behavior. Are you a person in charge of WSL in MS?

@aki-k
Copy link

aki-k commented Nov 29, 2023

@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 ~

@parapilot
Copy link

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.

@aki-k
Copy link

aki-k commented Dec 27, 2023

@parapilot I wrote about the automatic WSL 2 start at system startup for both Windows service method and for Task Scheduler method here:

#9231 (comment)
#9231 (comment)

@imshvc
Copy link

imshvc commented Apr 17, 2024

How long do developers need to endure this type of nonsense?

@aki-k
Copy link

aki-k commented Apr 17, 2024

@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

@pedrohgmacedo
Copy link

@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%

@aki-k
Copy link

aki-k commented Apr 17, 2024

@pedrohgmacedo I have "even" tested it. The option says Ubuntu, not Debian, so install it if you don't have dbus-launch.

@aki-k
Copy link

aki-k commented Apr 18, 2024

What in the world is happening? I'm providing help here and get thumbs down?

@kacprawcze
Copy link

Cmon Microsoft, this is a huge PITA, but a simple thing to implement. Please add a setting to completely disable the shutdown and/or configure the idle timeout.

@aki-k
Copy link

aki-k commented May 22, 2024

@kacprawcze Here's the solution:

C:\>wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true & wsl.exe --distribution Ubuntu-22.04

@kacprawcze
Copy link

@kacprawcze Here's the solution:

C:>wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true & wsl.exe --distribution Ubuntu-22.04

Thank you, but a lot of people posted that already. And that's not a solution, that's a nasty workaround.

@aki-k
Copy link

aki-k commented May 22, 2024

@kacprawcze So instead of starting WSL 2 like that, solving the problem, you instead want to wait for Microsoft to fix it what they are not interested in doing?

"Nasty workaround" lol

@lasorda
Copy link

lasorda commented Jun 16, 2024

I found a solution with Install docker destktop, It't enable Enable integration with my default WSL distro default

@Parsifa1
Copy link

Parsifa1 commented Jul 23, 2024

@kacprawcze Here's the solution:
C:>wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true & wsl.exe --distribution Ubuntu-22.04

Thank you, but a lot of people posted that already. And that's not a solution, that's a nasty workaround.

Why is this a nasty workaround?
i have been using it for a long time

just add dbus-launch true to you .bashrc or config.fish or any other shell config

@aki-k
Copy link

aki-k commented Jul 23, 2024

"Nasty workaround" lol
6 thumbs down

To all the haters; it should be obvious by now to everyone that Microsoft has no plan to fix this issue.

@antispace2005
Copy link

antispace2005 commented Sep 16, 2024

a workaround I found is starting a screen and it did not shutdown for me

@SapitoSucio
Copy link

SapitoSucio commented Oct 7, 2024

It's weird because, If I execute the command wsl.exe -d rathena -e bash -c "exec bash && dbus-launch true" it will shut down itself 10s after I close the terminal, BUT, if I only execute the command wsl.exe --d rathena -e bash -c "dbus-launch true" no console will pop up, but I can open a new one, tinker with it, close the terminal and the distro won't be shut down... like why?

After some testing I found something else; Apparently If I close the terminal by clicking on the X button it's when WSL will shutdown, but if I execute exit then the WSL continues without any problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests