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 · 19 comments
Closed

How to make wsl2 alive in the background #10138

amuncx opened this issue May 25, 2023 · 19 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.

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

6 participants