systemd 进程管理

2022-03-04  本文已影响0人  _于曼丽_

systemd 与 init 对比

CentOS 7 用 systemd 取代了 init,来管理系统的进程。

1. init 与 systemd

init 进程启动流程(串行启动):

  1. 系统调用 init 进程作为第一个进程,pid = 1
  2. init 进程读取 /etc/inittab 文件,确定 runlevel,假设 runlevel 为 3
  3. 按顺序调用 /etc/rc.d/rc3.d/ 目录中所有以 S 开头的软连接所指向的启动脚本
  4. 启动脚本中会调用位于 /usr/sbin/ 或者 /usr/bin/ 目录中的对应的二进制程序
  5. 启动二进制程序,会开启一个或多个进程

systemd 进程启动流程(并行启动):

  1. 系统调用 systemd 进程作为第一个进程,pid = 1
  2. systemd 进程查看 /etc/systemd/system/default.target 软连接所指向的 target,假设为 multi-user.target
  3. 读取 /etc/systemd/system/multi-user.target.wants/ 目录中所有软连接所指向的配置文件
  4. 如果该 unit 是 service 类型的,根据配置文件中的 [Service] 区块中的 ExecStart 选项启动对应的二进制程序
  5. 启动二进制程序,会开启一个或多个进程

2. RunLevel 与 Target

RunLevel 与 Target 的对应关系如下:

Traditional runlevel      New target name     Symbolically linked to...
Runlevel 0           |    runlevel0.target -> poweroff.target
Runlevel 1           |    runlevel1.target -> rescue.target
Runlevel 2           |    runlevel2.target -> multi-user.target
Runlevel 3           |    runlevel3.target -> multi-user.target
Runlevel 4           |    runlevel4.target -> multi-user.target
Runlevel 5           |    runlevel5.target -> graphical.target
Runlevel 6           |    runlevel6.target -> reboot.target

现在也可以通过 systemctl set-default 命令设置 default.target 的指向。

# 设置开机启动级别为 3
systemctl set-default multi-user.target
# 设置开机启动级别为 5
systemctl set-default graphical.target
# 查看开机启动级别
systemctl get-default

3. 启动脚本与配置文件

以前是通过启动脚本来调用对应的二进制程序,现在是根据配置文件中 [Service] 块中的 ExecStart 选项来调用对应的二进制程序。

Unit

Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。

Unit 一共分成12种类型:

我们最常使用的是 service 类型。

配置文件

每个 Unit 都有一个配置文件,告诉 Systemd 怎么启动这 Unit。配置文件的后缀名,代表着 Unit 类型,比如 sshd.socket。如果省略,Systemd 的默认后缀名为 service,所以 sshd 会被理解成 sshd.service

systemctl enable 命令用于在上面两个目录之间,建立符号链接关系。
systemctl disable 命令用于在两个目录之间,撤销符号链接关系。

配置文件的状态

每个配置文件有四种状态:

注意配置文件的状态和 Unit 的状态是两个概念,Unit 的状态包含了配置文件的状态和其他状态,从配置文件的状态无法看出该 Unit 是否正在运行。

systemctl status httpd.service 显示单个 Unit 的状态
systemctl list-unit-files | grep httpd.service 显示配置文件的状态

一旦修改配置文件,就要让 SystemD 重新加载配置文件,然后重新启动该配置文件对应的 Unit,否则修改不会生效。

systemctl daemon-reload
systemctl restart httpd.service

配置文件的内容

systemctl cat 命令可以查看配置文件的内容

$ systemctl cat atd.service

[Unit]
Description=ATD daemon

[Service]
Type=forking
ExecStart=/usr/bin/atd

[Install]
WantedBy=multi-user.target

配置文件分成几个区块,每个区块由区块名和键值对组成,区块名和键值对都区分大小写,键值对 = 两边不能有空格。

[Service]
ExecStart: 执行 systemctl start 命令时候,调用的二进制程序

[Install]
WantedBy:表示该服务所在的 Target,也就是定义在哪个级别需要开机启动

详细参考

常用命令

# 查看当前系统的所有 Unit
systemctl list-units
# 查看当前系统的所有类型为 service 的 Unit
systemctl list-units --type=service
# 查看所有 Unit 的配置文件
systemctl list-unit-files
# 查看所有类型为 service 的 Unit 的配置文件
systemctl list-unit-files --type=service
# 显示某个 unit 的状态
systemctl status httpd.service
# 显示某个 unit 是否正在运行
systemctl is-active httpd.service
# 显示某个 unit 是否开机启动
systemctl is-enabled httpd.service
# 开机启动
systemctl enable httpd.service
# 取消开机启动
systemctl disable httpd.service
# 启动服务
systemctl start httpd.service
# 停止服务
systemctl stop httpd.service
# 杀死服务的所有进程
systemctl kill httpd.service
# 重启服务
systemctl restart httpd.service
# 查看配置文件的内容
systemctl cat httpd.service
# 重新加载所有修改过的配置文件
systemctl daemon-reload
# 查看开机启动级别
systemctl get-default
# 设置开机启动级别为 3,重启之后生效
systemctl set-default multi-user.target
# 设置开机启动级别为 5
systemctl set-default graphical.target
# 立即进入 3 级别,立即生效,不需要重启
systemctl isolate multi-user.target
# 立即进入 5 级别
systemctl isolate graphical.target
上一篇 下一篇

猜你喜欢

热点阅读