WSL icon indicating copy to clipboard operation
WSL copied to clipboard

How to make wsl2 alive in the background

Open amuncx opened this issue 3 years ago • 50 comments

Wsl2 will automatically shut down when the terminal is closed.I need it alive in the background. Is there such a function in the configuration file, so that wsl2 can only be closed manually by running wsl --shutdown. If not, would you consider adding this feature.

amuncx avatar May 25 '23 08:05 amuncx

@amunv WT has a headless feature (https://github.com/microsoft/terminal/pull/14944) that might keep your WSL instance running.

elsaco avatar May 25 '23 14:05 elsaco

@amunv WT has a headless feature (microsoft/terminal#14944) that might keep your WSL instance running.

I don't understand it a bit, should I add "allowHeadless": true in setting.json As he said, there is no wt --headless or wt --hidden.

amuncx avatar May 26 '23 08:05 amuncx

@amunv You can make it stay running with this setup: https://github.com/microsoft/WSL/issues/10157#issuecomment-1578024408

aki-k avatar Jun 06 '23 08:06 aki-k

Thank you for reporting this @amunv. You can force WSL2 to keep running with:

[wsl2]
vmIdleTimeout=-1

in %userprofile%/.wslconfig

OneBlue avatar Jun 13 '23 21:06 OneBlue

vmIdleTimeout=-1

I'm not sure why this is not documented at https://learn.microsoft.com/en-us/windows/wsl/wsl-config

It just says

The number of milliseconds that a VM is idle, before it is shut down. Only available for Windows 11.

aki-k avatar Jun 14 '23 01:06 aki-k

Thank you for reporting this @amunv. You can force WSL2 to keep running with:

[wsl2]
vmIdleTimeout=-1

in %userprofile%/.wslconfig

This is not what I need. About ten seconds after closing the terminal, wsl enters the suspended state. At this time, the docker application cannot be used normally, and the system memory usage is reduced. The "vmIdleTimeout" option controls the survival time of vmmemwsl after wsl is suspended. During this time, when WT is turned on, wsl will run normally. I don't need this suspend state and control the time after suspend. What I need is that wsl suspend after about ten seconds, disable this suspend. [wsl2] vmIdleTimeout=120000 memory=2GB processors=2

amuncx avatar Jun 14 '23 03:06 amuncx

Thank you for reporting this @amunv. You can force WSL2 to keep running with:

[wsl2]
vmIdleTimeout=-1

in %userprofile%/.wslconfig

This is not what I need. About ten seconds after closing the terminal, wsl enters the suspended state. At this time, the docker application cannot be used normally, and the system memory usage is reduced. The "vmIdleTimeout" option controls the survival time of vmmemwsl after wsl is suspended. During this time, when WT is turned on, wsl will run normally. I don't need this suspend state and control the time after suspend. What I need is that wsl suspend after about ten seconds, disable this suspend. [wsl2] vmIdleTimeout=120000 memory=2GB processors=2

Yes, that's exactly what vmIdleTimeout=-1 does. When set to -1, the WSL2 VM will never time out and WSL2 will keep running until wsl.exe --shutdown is called.

OneBlue avatar Jun 14 '23 22:06 OneBlue

@OneBlue Your solution does not work. I put

[wsl2]
vmIdleTimeout=-1

into C:\Users\aki\.wslconfig and restarted WSL 2 VM.

Then I installed apache2 in WSL 2 VM

sudo apt-get install apache2
sudo systemctl enable --now apache2

I can see in "ss -tulpan | grep apache2" that it's listening on port 80.

Then I load http://localhost/ on Windows host side Firefox and the Apache2 Ubuntu Default Page opens.

I exit from WSL 2 VM with "exit".

I have this loop running in another CMD prompt

for /l %i in () do @( tasklist | findstr /i "vm wsl" & ping -n 3 127.0.0.1 >nul & echo --- )

It lists the processes matching "vm" and "wsl".

When I exit WSL 2 VM, the 2 wslhost.exe processes end after a short while.

The Apache2 Ubuntu Default Page on the Windows host side Firefox stops responding at the same time.

aki-k avatar Jun 15 '23 09:06 aki-k

@aki-k vmIdleTimeout = -1 does keep the VM running after exiting the shell. You stated adding the setting then restarted WSL 2 VM. Make sure you restart the WSL service not only the VM. Run wsl --shutdown for the setting to take effect.

elsaco avatar Jun 15 '23 15:06 elsaco

and restarted WSL 2 VM.

means I exited the WSL 2 VM and ran wsl --shutdown in CMD prompt. Do I also need to restart lxssmanager?

aki-k avatar Jun 15 '23 15:06 aki-k

After "exit" -> 2 wslhost.exe processes stop after a short while -> "bash" -> I can use "ps auxwwf | grep apache2" to see the process start-up time and they just started up. So the WSL 2 instance also started up again.

aki-k avatar Jun 15 '23 16:06 aki-k

I actually now found a command that starts WSL 2 VM, exits, and WSL 2 VM and instance are left running:

wsl --exec dbus-launch true

aki-k avatar Jun 15 '23 23:06 aki-k

I actually now found a command that starts WSL 2 VM, exits, and WSL 2 VM and instance are left running:

wsl --exec dbus-launch true

This command is very useful, but I have another problem about WT.The command line of Profiles is run by cmd in windows terminal.The exec parameter cannot be recognized normally in cmd, but it can be recognized in powershell.Do you have an idea to change the command line to pwsh, or let cmd recognize "-e"?

amuncx avatar Jun 17 '23 11:06 amuncx

@amunv

The exec parameter cannot be recognized normally in cmd, but it can be recognized in powershell.

I don't use Windows Terminal, just CMD prompt (with clink) and it works in it.

aki-k avatar Jun 17 '23 11:06 aki-k

@amunv

The exec parameter cannot be recognized normally in cmd, but it can be recognized in powershell.

I don't use Windows Terminal, just CMD prompt (with clink) and it works in it.

Sorry,I just misunderstood this command, I thought this would start the wsl instance.

amuncx avatar Jun 17 '23 11:06 amuncx

@amunv Yes, it starts the WSL 2 VM and the WSL 2 instance.

aki-k avatar Jun 17 '23 11:06 aki-k

@amunv i.e.

C:\Users\aki>wsl --exec dbus-launch true & bash

aki-k avatar Jun 17 '23 15:06 aki-k

I actually now found a command that starts WSL 2 VM, exits, and WSL 2 VM and instance are left running:

wsl --exec dbus-launch true

Savior for real. In most cases the timeout = -1 or dbus-launch true should work.

for context, if anyone cares ,I am trying to run a ssh-server to login into WSL2 from my macbook. This was the final piece of the puzzle <3. Thanks much.

BarathwajAnandan avatar Dec 11 '23 00:12 BarathwajAnandan

@aki-k wsl --exec dbus-launch true

This works for me, the wsl stays running. But After a reboot do you need to put the command in again? After I rebooted it said it was running but after a minute it said it stopped.

Ali-thepro avatar Mar 28 '24 23:03 Ali-thepro

I actually now found a command that starts WSL 2 VM, exits, and WSL 2 VM and instance are left running:

wsl --exec dbus-launch true

For Windows 10 (because the vmIdleTimeout = -1 method only works for Windows 11), we can further refine @aki-k's method above to make the process automatic by adding the following bash commands to your wsl distro, e.g., Ubuntu's .bashrc file:

#-----------------------
# keep wsl distro alive
#-----------------------
if ! pgrep -u "$(whoami)" -x "dbus-daemon" > /dev/null; then
   dbus-launch true
fi

The pgrep line is to ensure that we don't create multiple dbus processes for this purpose (1 process suffices).

MRBZ-GIT avatar May 20 '24 23:05 MRBZ-GIT