There are some good answers here, and I hate to add another, but I think they are all missing something fairly basic -- WSL instances will automatically terminate when no processes are running.
As I note (in more detail) in this Github discussion, there are two timers that control when WSL terminates or shuts down:
When no process is running other than /init
inside a distribution/instance, the distribution will terminate. That's the equivalent of a wsl.exe --terminate $WSL_DISTRO_NAME
. This timer is hardcoded to 15 seconds.
If no WSL2 distributions are running in the RUNNING
state (as seen by wsl.exe -l -v
), then the WSL2 VM itself is shut down. This is the equivalent of a wsl.exe --shutdown
.
So really, in most cases, there's no need to manually call any additional command to shutdown WSL.
Do I have to just press X every time when I want to exit?
You can, if you'd like. When the terminal window closes, the shell and any processes running in it will close as well. After a few seconds, if no processes are running, WSL will automatically terminate the instance based on the timer mentioned above.
But you can accomplish the same thing by simply exiting your shell (assuming that no background processes are running). Either type exit
Enter or, in most cases, Ctrl+D to exit the shell. Again, after a few seconds, if no other processes are running in the instance, WSL will automatically terminate it.
You can see this in action by doing something simple like:
- Start WSL Ubuntu
- In PowerShell or CMD, run
wsl -l -v
. The "Ubuntu" instance will show as "Running"
- In your shell (likely Bash), just press Ctrl+D
- Quickly run
wsl -l -v
again, and the Ubuntu instance will likely still show are "Running"
- Wait 20 seconds, run
wsl -l -v
again, and the Ubuntu instance will likely show as "Stopped"
If it doesn't, then something may still be running. You can check this with wsl -e ps aux
(again, from PowerShell or CMD). If nothing else is running, you'll see two init
processes and the ps
process only:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 8940 316 ? Ssl 19:34 0:00 /init
root 6 0.0 0.0 8940 224 tty1 Ss 19:34 0:00 /init
username 7 0.0 0.0 17392 1904 tty1 R 19:34 0:00 ps auxw
If anything else shows up, then WSL probably won't terminate the instance automatically.
In that case, Pablo's excellent answer comes into play. You can forcibly terminate the instance via wsl.exe --terminate Ubuntu
(or whatever your distribution is named). All processes inside the instance will be terminated.
Or (also covered by Pablo), you can shut down the entire WSL system with wsl.exe --shutdown
. This takes down all instances (and processes running in them) as well as the networking system and interop. You'll notice a slightly longer delay when starting a fresh WSL instance after a --shutdown
.
Shutdown from inside Ubuntu
Note that this can also work from inside Ubuntu/WSL by simply calling wsl.exe
instead of just wsl
. This will terminate the currently running WSL distribution:
wsl.exe --terminate $WSL_DISTRO_NAME
And this will shut down all running distributions and the WSL2 VM itself (from inside Ubuntu):
wsl.exe --shutdown
However, please note that those are not "graceful" shutdowns. All processes that were still running will be immediately terminated with no change to respond to any signal.
Graceful shutdown under Windows 11
Under recent WSL releases in Windows 11, you can now run Systemd. Services started by Systemd under WSL2 are automatically terminated when no other processes (other than those started by Systemd, essentially) are running in Ubuntu.
This shutdown is graceful. WSL2 communicates with Systemd to have it gracefully stop (via normal Systemd signal behavior) any service running under it.
This still isn't necessarily optimal, since you need one additional process running in the background (started "interactively") to keep WSL running in the meantime. See this answer for a solution on how to do this. If you use something like keychain
as I mention there, then you just need to stop that process (keychain -k all
), exit the shell in Ubuntu, and within 15 or so WSL2 should send the "graceful shutdown" signal to Systemd.
Why normal Linux shutdown mechanisms won't work in WSL
So why don't normal reboot
/shutdown
commands work? Mainly because Ubuntu under WSL isn't running in a physical or virtual machine. What you are running is actually an Ubuntu container inside a managed (in other words, we can't normally interact with it directly) WSL2 VM.
Attempting to "shut down" a WSL distribution would be like attempting to "shut down" a Docker/Podman/other container. If Ubuntu is running in Docker, you don't shut it down from Ubuntu, but by either:
- Executing a
docker stop
command from outside the container
- Or simply exiting all running processes inside the container
It's the same with WSL.
wsl -l -v
towsl --shutdown
tonet stop LxssManager
and they all failed. The last one ended up in "the service is starting or stopping" - the purpose of this comment is to point out that this question still may not be fully answered.logout
wsl --shutdown
in a PowerShell or cmd prompt