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.
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.
But you can accomplish the same thing by simply exiting your shell (assuming 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 10-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 --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 --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
.
So why don't normal reboot
/shutdown
commands work? Two reasons. First, as covered by Bengt, WSL doesn't currently support systemd, and Ubuntu simply links these two legacy commands to /usr/bin/systemctl
(the systemd control utility). You can see this with ls -l /usr/sbin
.
But even if these were the legacy commands which directly called Linux's shutdown API, it wouldn't work. Microsoft doesn't typically hook up API's that interact directly with the hardware, instead providing virtualization interfaces where necessary. But in the case of starting and stopping a WSL instance, it's just so "lightweight" (as discussed above) that there's not any real reason to do so.
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