init和systemd

init和systemd是什么?

首先,initsystemd都是用来启动Linux操作系统的,他们都是操作系统kernel发起的第一个进程。只不过有一些Linux发行版用的是init,有一些用的是systemd

严格来说,在initsystemd之前还有BIOS/UEFI,他们执行硬件初始化,然后确定下一个运行的程序,比如说我的电脑安装了双系统,所以会有grub来让我选择加载Ubuntu还是Windows。而在我选择Ubuntu之后,systemd就是内核运行的第一个程序,这就是为什么他的PID是1。

init

是早期Linux系统启动程序。启动脚本写在/etc/init.d文件夹下。使用时直接调用,如/etc/init.d/apache2 start

  1. 启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  2. 启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

systemd

并行启动程序,克服了init的一些缺点。新的很多Linux发行版用的都是这个,比如Ubuntu和CentOS。systemctl是它用来进行系统管理的工具。

# 重启系统
$ sudo systemctl reboot

# 关闭系统,切断电源
$ sudo systemctl poweroff

# 显示系统状态
$ systemctl status

# 显示单个 Unit 的状态
$ sysystemctl status bluetooth.service

# CPU停止工作
$ sudo systemctl halt

# 暂停系统
$ sudo systemctl suspend

# 让系统进入冬眠状态
$ sudo systemctl hibernate

# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep

# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue

service命令

是为了兼容上面两个系统的命令,会在下面几个目录中搜索可用的服务:

  1. /etc/init.d: The directory containing System V init scripts.
  2. /{lib,run,etc}/systemd/system: The directories containing systemd units.

Reference

  1. https://www.tecmint.com/systemd-replaces-init-in-linux/

  2. https://bbs.huaweicloud.com/blogs/218845

  3. https://unix.stackexchange.com/questions/519250/splash-in-pid-1

· Operating System