systemd 相关命令汇总

1. 获取 units 信息

通过 systemctl list-units 可以快速获取获取所有 units 的信息。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#列出正在运行的 Unit
systemctl list-units
# 列出所有Unit,包括没有找到配置文件的或者启动失败的
systemctl list-units --all
# 列出所有没有运行的 Unit
systemctl list-units --all --state=inactive
# 列出所有加载失败的 Unit
systemctl list-units --failed
# 列出所有正在运行的、类型为 service 的 Unit
systemctl list-units --type=service

通过 systemctl list-dependencies 可以获取 units 的依赖信息。

1
2
3
4
# 获取 ng 服务的依赖树
systemctl list-dependencies nginx.service
# 获取 ng 服务的依赖树的详细信息
systemctl list-dependencies --all nginx.service

2. units 状态管理

通过 systemctl 管理 unit 状态(主要是 service 类型的 unit),是用户最为常见的操作:

1
2
3
4
5
6
7
8
# 获取所有 units 状态
systemctl status
# 显示单个 Unit 的状态
sysystemctl status bluetooth.service
# 快速诊断 unit 状态
systemctl is-active/is-failed/is-enabled application.service
# 管理单个 units 状态
systemctl start/stop/restart/kill apache.service

3. units 配置管理

通过 systemctl 用户可以快速添加/删除,units 的配置信息以及环境变量信息。 这些额外添加的配置会以文件的形式保存在 /etc/systemd/system.control/<unit-name>.<unit-type>.d/ 目录下:

1
2
3
4
5
6
7
8
# 打印所有配置文件
systemctl cat httpd.service
# 显示某个 Unit 的所有底层参数
systemctl show httpd.service
# 显示某个 Unit 的指定属性的值
systemctl show -p CPUShares httpd.service
# 设置某个 Unit 的指定属性
systemctl set-property httpd.service CPUShares=500

用户手工编辑 units 配置文件时,需要通过以下命令重载配置

1
2
3
4
# 重新加载一个服务的配置文件
systemctl reload apache.service
# 重载所有修改过的配置文件
systemctl daemon-reload

4. 环境变量管理

systemd 有独立的环境变量管理系统,用户有以下几种方式将环境变量注入到 systemd 管理的进程中:

  • 用户定义 service unit 配置时使用 Environment 或者 EnvironmentFile 针对单个 unit 注入;
  • 通过 systemd 全局/user 环境变量注入,其中管理这两种环境变量有以下几种方式:
    • 通过 /etc/systemd/system.conf 和 /etc/systemd/user.conf 中的 DefaultEnvironment 参数设置环境变量(重启系统生效);
    • 通过 /usr/lib/systemd/system-environment-generators/ 和 /usr/local/lib/systemd/user-environment-generators/ 目录中的自定义脚本生成环境变量(参考),用户放置脚本后可以通过 systemctl daemon-reload 生效
    • 通过以下命令管理 systemd 的全局环境变量或者 user 级别环境变量(加上 –user 参数),但是这种方式生成的变量是临时生效的。
1
2
3
4
5
6
7
8
# 获取环境变量
systemctl show-environment
# 设置环境变量
systemctl set-environment NAME1=VALUE1  NAME2=VALUE2
# 移除环境变量
systemctl unset-environment NAME1 NAME2
# 将当前 shell 中的环境变量导入到 systemd 中
systemctl import-environment NAME1 NAME2

5. 远程管理

systemctl 能够基于 sshd 管理远程主机上的 units。用户可以通过 passwd 或免密配置执行任意指令。 但是 systemctl 没有提供参数来指定对端的 sshd 端口,用户修改 /etc/ssh/ssh_config 指定端口号

1
systemctl -H root@<address> status httpd.service

6. 管理操作系统

通过 systemctl 管理系统的重启、暂停等操作。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 重启系统
$ sudo systemctl reboot
# 关闭系统,切断电源
$ sudo systemctl poweroff
# CPU停止工作
$ sudo systemctl halt
# 暂停系统
$ sudo systemctl suspend
# 让系统进入冬眠状态
$ sudo systemctl hibernate
# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep
# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue

6. 其他令行工具

systemd-analyze 提供了系统启动时间、启动流程的分析工具。

1
2
3
4
5
6
7
8
# 查看启动耗时
systemd-analyze                                                                                       
# 查看每个服务的启动耗时
systemd-analyze blame
# 显示瀑布状的启动过程流
systemd-analyze critical-chain
# 显示指定服务的启动流
systemd-analyze critical-chain atd.service

参考