Cervol

Cervol

Keep wsl2 running in the background

The original requirement was to remotely connect to the WSL2 on a Windows host via SSH using a Mac, mainly referring to this tutorial. During debugging, it was found that the key issue was to keep WSL2 running in the background; once the WSL2 instance is stopped, it cannot be connected.

Windows PowerShell can check the status of all WSL distributions using wsl -l -v.

The final solution referenced here, which is to run

wsl --exec dbus-launch true

It has been tested and is effective. By the way, after exiting a WSL instance, you can set an idle time for stopping the instance, with a default value of 60s, but it does not need to be modified to meet our needs; the above command is sufficient. This command essentially runs dbus-launch true in WSL, where dbus-launch is an auxiliary tool for D-Bus that starts a D-Bus process and returns environment variables. According to ChatGPT, certain D-Bus daemons will keep WSL running.

To avoid manually entering the command each time, you can consider creating it as a Windows Task Scheduler task. You can set it graphically by entering taskschd.msc with Win+R, or you can set it up more conveniently with a single command. Open PowerShell as an administrator and run:

schtasks /create /tn "AutoStartWSL" /tr "wsl --exec dbus-launch true" /sc ONLOGON /RL HIGHEST /F

The general meaning is to create a scheduled task that runs the above command at startup. You can check if the task was successfully created with:

Get-ScheduledTask -TaskName "AutoStartWSL"

To manually run the task:

Start-ScheduledTask -TaskName "AutoStartWSL"
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.