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

How to make wsl2 alive in the background #10138

Closed
amuncx opened this issue May 25, 2023 · 28 comments
Closed

How to make wsl2 alive in the background #10138

amuncx opened this issue May 25, 2023 · 28 comments
Labels

Comments

@amuncx
Copy link

amuncx commented May 25, 2023

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 amuncx added the feature label May 25, 2023
@elsaco
Copy link

elsaco commented May 25, 2023

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

@amuncx
Copy link
Author

amuncx commented May 26, 2023

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

@aki-k
Copy link

aki-k commented Jun 6, 2023

@amunv You can make it stay running with this setup: #10157 (comment)

@amuncx amuncx closed this as completed Jun 6, 2023
@amuncx amuncx reopened this Jun 6, 2023
@OneBlue
Copy link
Collaborator

OneBlue commented Jun 13, 2023

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

[wsl2]
vmIdleTimeout=-1

in %userprofile%/.wslconfig

@OneBlue OneBlue closed this as completed Jun 13, 2023
@aki-k
Copy link

aki-k commented Jun 14, 2023

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.

@amuncx
Copy link
Author

amuncx commented Jun 14, 2023

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

@OneBlue
Copy link
Collaborator

OneBlue commented Jun 14, 2023

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.

@aki-k
Copy link

aki-k commented Jun 15, 2023

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

@elsaco
Copy link

elsaco commented Jun 15, 2023

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

@aki-k
Copy link

aki-k commented Jun 15, 2023

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
Copy link

aki-k commented Jun 15, 2023

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
Copy link

aki-k commented Jun 15, 2023

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

@amuncx
Copy link
Author

amuncx commented Jun 17, 2023

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"?

@aki-k
Copy link

aki-k commented Jun 17, 2023

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

@amuncx
Copy link
Author

amuncx commented Jun 17, 2023

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

@aki-k
Copy link

aki-k commented Jun 17, 2023

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

@aki-k
Copy link

aki-k commented Jun 17, 2023

@amunv i.e.

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

@BarathwajAnandan
Copy link

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.

@Ali-thepro
Copy link

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

@MRBZ-GIT
Copy link

MRBZ-GIT commented May 20, 2024

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

@nodesocket
Copy link

Running Windows 11 Pro, and my .wslconfig is:

[wsl2]
vmIdleTimeout=-1
memory=8GB
processors=4
swap=0

However, when I close the WSL Ubuntu window my systemd services stop. Is there a way to continue to have them run when the Ubuntu window is closed?

@aki-k
Copy link

aki-k commented May 24, 2024

@nodesocket The solution (until Microsoft fixes it) is to start WSL 2 like this:

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

@nodesocket
Copy link

@nodesocket The solution (until Microsoft fixes it) is to start WSL 2 like this:

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

Thanks for the reply. I'm a macOS and Linux guy, is there a way to make the above a shortcut?

@aki-k
Copy link

aki-k commented May 24, 2024

@nodesocket Well try this :)

@echo off

wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true
wsl.exe --distribution Ubuntu-22.04

Put the above in start-wsl-2.bat in your %PATH% (CMD prompt) / $env:PATH (Powershell).

@aki-k
Copy link

aki-k commented May 27, 2024

@nodesocket Here's a better solution that doesn't open an extra CMD prompt/console window:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "wsl.exe --distribution Ubuntu-22.04 --exec dbus-launch true", 0, True
objShell.Run "wsl.exe --distribution Ubuntu-22.04", 1, True

^- start-wsl-2.vbs

@fliespl
Copy link

fliespl commented Aug 16, 2024

I have added there lines to .bashrc:

# Check if DBus is running, if not, launch it
if ! pgrep -x "dbus-daemon" > /dev/null; then
    eval `dbus-launch --sh-syntax`
    echo "DBus session started."
fi

@gardengim
Copy link

gardengim commented Sep 28, 2024

I just wrote a simple systemd service keeping WSL instance alive: keepwsl

@SapitoSucio
Copy link

SapitoSucio commented Oct 7, 2024

It's weird because, If I execute the command wsl.exe -d rathena -u froggo -e bash -c "exec bash && cd /home/froggo/rathena && dbus-launch true" it will shut down itself 10s after I close the terminal, BUT, if I only execute the command wsl.exe --distribution 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
Projects
None yet
Development

No branches or pull requests