在 WSL(Windows Subsystem for Linux,适用于 Linux 的 Windows 子系统)下通过systemctl命令管理某些服务将造成System has not been booted with systemd as init system (PID 1). Can't operate.这样的错误;

以启动 Nginx 为例:

systemctl start nginx
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

这表示你的系统不是以systemd启动的,无法操作;

可使用ps -p 1命令查看 PID 为 1 的进程:

ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:00 init(Debian)
  • 如果显示 init,表示系统正在使用传统的 SysV init;
  • 如果显示 systemd,表示系统正在使用 systemd;

你使用的systemctl命令是systemd系统管理器的核心控制工具,无法在SysV init下使用。

解决方法

将 WSL 子系统的初始化方式由init切换为systemd,具体操作方法请查看:

你还可以使用service命令作为替代管理工具,示例如下:

sudo service nginx start       # 启动
sudo service nginx stop        # 停止
sudo service nginx restart     # 重启
sudo service nginx reload      # 热重载
sudo service nginx status      # 查看状态
service --status-all           # 列出所有服务