23

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 or sudo 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.

4
  • 1
    I tried to apply all the suggestions provided in the answers - from wsl -l -v to wsl --shutdown to net 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. Nov 18, 2021 at 12:09
  • Why do you want to shutdown? When you can simply logout
    – Atif Ali
    Jul 22, 2022 at 16:25
  • @AtifAli how about "it consumes memory"? Oct 31, 2022 at 11:14
  • If you care about that memory, you can always use wsl --shutdown in a PowerShell or cmd prompt
    – Atif Ali
    Nov 7, 2022 at 7:51

6 Answers 6

25

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!

1
  • 1
    wsl --shutdown Ubuntu worked for me
    – B T
    Dec 15, 2022 at 2:18
11

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.

0
6

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.

4
  • 1
    When 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.
    – Peter Wone
    Nov 16, 2021 at 23:52
  • 1
    @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 the docker-desktop instance is still running until you manually stop either Docker Desktop, wsl --terminate docker-desktop, or wsl --shutdown. Nov 17, 2021 at 0:47
  • WSL2 does support systemD. More over, we don't need Windows command prompt or powershell to run wsl command, just wsl.exe --shutdown will do it from the ubuntu terminal. If you've disabled windows paths, you can add it back as an alias from /mnt/c/Windows/system32/wsl.exe
    – Ray Foss
    Aug 10, 2022 at 16:10
  • 1
    @RayFoss Fair enough, I've edited to clarify. I would, however, disagree that WSL2 "supports" Systemd. The WSL team still considers it unsupported, with additional features that would be needed to consider it "supported". As you seem to be aware, it's possible to run it inside a PID namespace where it can run as PID1, but there are almost always corner-case issues that arise from that (as seen on something like the Genie issue tracker. Aug 10, 2022 at 16:25
2

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.

  1. Open cmd.
  2. Use wsl -l or wsl --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
    
  3. Terminate / shutdown your desired distribution using wsl -t or wsl --terminate like
wsl --terminate Ubuntu

and it will start automatically next time when you open it.

1
  • I 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. Jul 21, 2021 at 20:42
0

systemd support is not implemented. The best way (I have found) is to reboot the whole WSL subsystem: As admin start a cmd shell and

net stop LxssManager
net start LxssManager
1
  • Hi Bengt, I am curious if you have any advice for a situation when windows is not able to restart the service. Nov 18, 2021 at 12:03
0

If you want to exit and close the WSL terminal you can simply type: logout or exit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .