sco*_*rod 8 reboot windows shutdown windows-subsystem-for-linux
我刚从 Microsoft Store 获得 Ubuntu 并尝试了一些东西。但是,当我尝试重新启动或关闭系统时,sudo reboot或者sudo shutdown -h我收到提示说
“系统尚未使用 systemd 作为 init 系统(PID 1)启动。无法操作。无法与 init 守护程序对话。”
这是否意味着我不能简单地关闭机器,因为它在 WSL 下?
每次我想退出时都必须按X吗?
我已经获得了 Windows 和 Ubuntu 的最新更新。
小智 15
有时wsl --shutdown似乎不起作用,例如运行这些命令:
wsl --shutdown
wsl -l -v
Run Code Online (Sandbox Code Playgroud)
导致一些容器仍在运行:
NAME STATE VERSION
* docker-desktop-data Stopped 2
docker-desktop Stopped 2
Ubuntu-20.04 Running 2
Run Code Online (Sandbox Code Playgroud)
结果是,当您尝试访问 WSL 驱动器时,WSL 实例会自动启动。就我而言,\\wsl$\Ubuntu-20.04\workspace它在 IDE 中打开,导致实例在关闭后立即重新启动。关闭 IDE 后,我能够正确关闭 WSL。
编辑:
这实际上是一个非常好的功能,我不必手动启动 WSL,我只需打开我的 IDE,它就会找到它需要的驱动器。
小智 13
我知道这是旧的,但刚刚发现你可以通过 powershell 做到这一点
列表分布:
wsl --list
Run Code Online (Sandbox Code Playgroud)
将输出类似
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
docker-desktop-data
docker-desktop
Run Code Online (Sandbox Code Playgroud)
终止 Ubuntu
wsl --terminate Ubuntu
Run Code Online (Sandbox Code Playgroud)
或终止所有
wsl --shutdown
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
这里有一些很好的答案,我不想添加另一个答案,但我认为它们都缺少一些相当基本的东西——当没有进程运行时, WSL 实例将自动终止。
正如我在此 Github 讨论中(更详细地)指出的,有两个计时器控制 WSL 何时终止或关闭:
当除了/init分发/实例内部没有任何进程在运行时,分发将终止。这相当于一个wsl.exe --terminate $WSL_DISTRO_NAME. 该计时器被硬编码为 15 秒。
如果没有 WSL2 发行版在该状态下运行RUNNING(如 所示wsl.exe -l -v),则 WSL2 VM 本身将关闭。这相当于一个wsl.exe --shutdown.
因此,实际上,在大多数情况下,无需手动调用任何其他命令来关闭 WSL。
我每次想退出时都必须按X吗?
如果你愿意的话,你可以。当终端窗口关闭时,外壳和其中运行的任何进程也将关闭。几秒钟后,如果没有进程在运行,WSL 将根据上述计时器自动终止实例。
但是您可以通过简单地退出 shell 来完成同样的事情(假设没有后台进程正在运行)。要么键入exit Enter,要么在大多数情况下键入Ctrl+D来退出 shell。同样,几秒钟后,如果实例中没有其他进程正在运行,WSL 将自动终止它。
您可以通过执行以下简单操作来查看其实际效果:
wsl -l -v. “Ubuntu”实例将显示为“正在运行”wsl -l -v,Ubuntu 实例可能仍会显示“正在运行”wsl -l -v再次运行,Ubuntu 实例可能会显示为“已停止”如果没有,那么某些东西可能仍在运行。您可以使用wsl -e ps aux(再次从 PowerShell 或 CMD)进行检查。如果没有其他进程在运行,您将看到两个init进程,并且ps仅看到该进程:
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
Run Code Online (Sandbox Code Playgroud)
如果出现其他任何情况,则 WSL 可能不会自动终止实例。
在这种情况下,巴勃罗的出色回答就发挥了作用。wsl.exe --terminate Ubuntu您可以通过(或您的发行版的任何名称)强制终止实例。实例内的所有进程都将被终止。
或者(Pablo 也介绍过),您可以使用 关闭整个 WSL 系统wsl.exe --shutdown。这会关闭所有实例(以及在其中运行的进程)以及网络系统和互操作。在--shutdown.
请注意,这也可以在Ubuntu/WSL内部工作,只需调用wsl.exe而不是仅仅调用wsl. 这将终止当前正在运行的 WSL 发行版:
wsl.exe --terminate $WSL_DISTRO_NAME
Run Code Online (Sandbox Code Playgroud)
这将关闭所有正在运行的发行版和 WSL2 VM 本身(从 Ubuntu 内部):
wsl.exe --shutdown
Run Code Online (Sandbox Code Playgroud)
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.
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.
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:
docker stop command from outside the containerIt's the same with WSL.
未实现 systemd 支持。最好的方法(我发现)是重新启动整个 WSL 子系统:以管理员身份启动 cmd shell 并
net stop LxssManager
net start LxssManager
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21355 次 |
| 最近记录: |