How to make wsl2 alive in the background
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.
@amunv WT has a headless feature (https://github.com/microsoft/terminal/pull/14944) that might keep your WSL instance running.
@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.
@amunv You can make it stay running with this setup: https://github.com/microsoft/WSL/issues/10157#issuecomment-1578024408
Thank you for reporting this @amunv. You can force WSL2 to keep running with:
[wsl2]
vmIdleTimeout=-1
in %userprofile%/.wslconfig
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.
Thank you for reporting this @amunv. You can force WSL2 to keep running with:
[wsl2] vmIdleTimeout=-1in %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
Thank you for reporting this @amunv. You can force WSL2 to keep running with:
[wsl2] vmIdleTimeout=-1in %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 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 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.
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?
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.
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
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"?
@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.
@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.
@amunv Yes, it starts the WSL 2 VM and the WSL 2 instance.
@amunv i.e.
C:\Users\aki>wsl --exec dbus-launch true & bash
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.
@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.
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).