7

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.

12

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!

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.

0
1

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.

1

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. – NotTheDr01ds Jul 21 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.