Can't restart/shutdown Ubuntu under WSL
Solution 1
I know this is old but just found that you can do it through powershell
list distributions:
wsl --list
will output something like
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
docker-desktop-data
docker-desktop
terminate Ubuntu
wsl --terminate Ubuntu
or terminate all
wsl --shutdown
hope this helps!
Solution 2
Sometimes wsl --shutdown
doesn't seem to work, like running these commands:
wsl --shutdown
wsl -l -v
result in some containers still running:
NAME STATE VERSION
* docker-desktop-data Stopped 2
docker-desktop Stopped 2
Ubuntu-20.04 Running 2
Turns out the WSL instances are automatically started when you try to access the WSL drives. In my case, \\wsl$\Ubuntu-20.04\workspace
was open in an IDE, and it caused the instance to restart immediately after shutdown. After I closed the IDE I was able to properly shut down WSL.
EDIT:
It is actually a very nice feature, I don't have to manually start WSL, I just open my IDE and it finds the drive it needs.
Solution 3
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.
Solution 4
Quoting my SO Answer
Assuming your wsl distribution name is Ubuntu
.
You can use wsl
command in Command Prompt
(cmd) to find out distribution names and terminate / shutdown / restart a specific distribution.
Restart in sense that you shutdown your wsl distribution and start it again.
- Open cmd.
- Use
wsl -l
orwsl --list
to list / show all installed distributions. It'll give you output like this. The(Default)
is not part of name, just a marker.Windows Subsystem for Linux Distributions: Ubuntu (Default) Ubuntu-20.04 Ubuntu-18.04
- Terminate / shutdown your desired distribution using
wsl -t
orwsl --terminate
like
wsl --terminate Ubuntu
and it will start automatically next time when you open it.
scotrod
Updated on September 18, 2022Comments
-
scotrod over 1 year
I just got Ubuntu from Microsoft Store and tried some things with it. However when i try to restart or shut down the system using
sudo reboot
orsudo shutdown -h
I got a prompt saying"System has not been booted with systemd as init system (PID 1). Can't operate. Failed to talk to init daemon."
Does this means that I can't simply power off the machine since it's under WSL?
Do i have to just press X every time when i want to exit?I've got the latest updates to Windows and Ubuntu.
-
Petr Chloupek over 2 yearsI tried to apply all the suggestions provided in the answers - from
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.
-
-
NotTheDr01ds almost 3 yearsI know this comes a year late, but this is just almost an exact duplicate of the answer that had already been provided 4 months prior from Pablo. I don't see that yours adds anything. At best, you should edit any additional information (if there is) into the existing answer.
-
Peter Wone over 2 yearsWhen Docker for Win uses WSL it consumes virtual memory, often quite a lot. When you shut down the container, this isn't released because it is held by Ubuntu which is still running.
wsl --shutdown 0
gets your RAM back. -
NotTheDr01ds over 2 years@PeterWone Well, sort of. You make a good point about Docker continuing to consume memory, but (a) that's a corner case (not even covered by this particular question), and (b) it's actually not the Ubuntu instance which is consuming the memory; it's the
docker-desktop
one. As per my answer, the Ubuntu instance will terminate (and release its memory) when no processes are running inside it, but thedocker-desktop
instance is still running until you manually stop either Docker Desktop,wsl --terminate docker-desktop
, orwsl --shutdown
. -
Petr Chloupek over 2 yearsHi Bengt, I am curious if you have any advice for a situation when windows is not able to restart the service.