Skip to content

SysVinit和Systemd对比

描述SysVinitSystemd
启动服务service example startsystemctl start example
停止服务service example stopsystemctl stop example
重新启动服务service example restartsystemctl restart example
重新加载服务配置文件service example reloadsystemctl reload example
查看服务状态service example statussystemctl status example
系统启动时启用服务chkconfig example onsystemctl enable example
系统启动时禁用服务chkconfig example offsystemctl disable example
打印服务列表chkconfig --listsystemctl list-unit-files --type=service

WSL2中遇到的问题

在WSL2中使用systemd命令时可能会遇到错误,如:

shell
System has not been booted with systemd as init system (PID 1). Can't operate.

原因:Linux系统可能没有使用systemd。

当你想用systemd命令来管理Linux上的服务时,你的系统可能并没有使用systemd,很可能使用的是经典的SysV init(sysvinit)。

这个情况在WSL上十分常见,

如何知道你使用的是哪个init系统?你可以使用这个命令来知道与PID 1(系统上运行的第一个进程)相关联的进程名称。

shell
ps -p 1 -o comm=

它应该在输出中显示systemd或sysv,或类似的东西。

如何解决System has not been booted with systemd错误?

  1. 启动systemd服务
  2. 使用等同的sysvinit命令

systemd和sysvinit的命令对照表:

描述SysVinitSystemd
启动服务service example startsystemctl start example
停止服务service example stopsystemctl stop example
重新启动服务service example restartsystemctl restart example
重新加载服务配置文件service example reloadsystemctl reload example
查看服务状态service example statussystemctl status example
系统启动时启用服务chkconfig example onsystemctl enable example
系统启动时禁用服务chkconfig example offsystemctl disable example
打印服务列表chkconfig --listsystemctl list-unit-files --type=service

SysVinit和Systemd对比

原文来自:SysVinit 和 Systemd 对比 · linuxcoming.com

systemd是一个全新的init系统和系统管理器,兼容传统的基于SysV init系统的所有主要的Linux发行版。 systemd兼容SysV和LSB init脚本,它作为SysV init系统的直接替代品。 systemd是内核启动并保持PID 1的第一个进程,它是系统运行后的所有进程的的父进程,Fedora 15是第一个支持systemd发行版。

systemctl是用于管理systemd守护程序/服务命令行实用程序和主要工具,例如(start, restart, stop, enable, disable, reload & status)。 systemd使用.service文件而不是bash脚本(SysV init使用)。systemd将所有守护进程排序到他们自己的Linux cgroup中,你可以通过搜索/cgroup/systemd文件来查看系统层次结构。

以下三个init系统在Linux中广泛使用。

  • System V (Sys V): System V(Sys V)是Unix操作系统的第一个和传统的init系统之一。
  • Upstart: Upstart是基于事件的/sbin/init守护程序。
  • systemd: Systemd是一个新的init系统和系统管理器,它通过传统的SysV init系统实现/适用于所有主要的Linux发行版。

什么是System V(Sys V)?

System V(Sys V)是Unix操作系统的第一个和传统的init系统之一。init是内核启动系统期间启动的第一个进程,它是所有内容的父进程。

大多数Linux发行版首先使用称为System V(Sys V)的传统初始化系统。多年来,发布了几个替换初始化系统,以解决标准版本中的设计限制,例如launchd,Service Management Facility,systemd和Upstart。

但是,与传统的SysV init系统相比,systemd已经被几个主要的Linux发行版所采用。

什么是systemd?

systemd是一个新的init系统和系统管理器,它已成为大多数Linux发行版中非常流行并广泛适应的新标准init系统。systemctl是一个systemd实用程序,它可以帮助我们管理systemd系统。

系统功能

  • systemd提供了强大的并行处理能力
  • 使用socket和D-Bus激活来启动服务
  • 提供按需启动(on-demand starting)守护进程
  • 使用Linux cgroup跟踪进程
  • 支持快照(snapshotting)和恢复(restoring)系统状态
  • 维护挂载和自动挂载点
  • 完善实现了基于事务依赖性的服务控制逻辑

常见操作对比

描述SysVinitSystemd
启动服务service example startsystemctl start example
停止服务service example stopsystemctl stop example
重新启动服务service example restartsystemctl restart example
重新加载服务配置文件service example reloadsystemctl reload example
查看服务状态service example statussystemctl status example
系统启动时启用服务chkconfig example onsystemctl enable example
系统启动时禁用服务chkconfig example offsystemctl disable example
打印服务列表chkconfig --listsystemctl list-unit-files --type=service

参考链接