Linux 与服务器运维 / 知识点
systemd Unit 配置速查手册
[Unit] 基础与依赖
Description=<TEXT>给 Unit 提供可读描述。
Description=
给 Unit 提供可读描述。
所属 Section
[Unit]
语法
Description=<TEXT>推荐示例
Description=Anystack API Service关键边界
会显示在 systemctl status、list-units 等输出中。
Documentation=<URI> [...]声明 Unit 相关文档地址。
Documentation=
声明 Unit 相关文档地址。
所属 Section
[Unit]
语法
Documentation=<URI> [...]推荐示例
Documentation=https://docs.example.com/app关键边界
可配置 man:、https: 等 URI;systemctl help <UNIT> 会利用它。
Wants=<UNIT> [...]弱依赖:启动当前 Unit 时,也尝试启动指定 Unit。
Wants=
弱依赖:启动当前 Unit 时,也尝试启动指定 Unit。
所属 Section
[Unit]
语法
Wants=<UNIT> [...]推荐示例
Wants=network-online.target关键边界
依赖启动失败通常不会阻止当前 Unit;它不决定启动顺序。
Requires=<UNIT> [...]强依赖:启动当前 Unit 时,同时拉起指定 Unit。
Requires=
强依赖:启动当前 Unit 时,同时拉起指定 Unit。
所属 Section
[Unit]
语法
Requires=<UNIT> [...]推荐示例
Requires=postgresql.service关键边界
比 Wants 更强,但仍需用 After/Before 单独配置顺序;多数业务依赖优先考虑 Wants。
Requisite=<UNIT> [...]要求目标 Unit 已经处于 active,否则当前 Unit 启动失败。
Requisite=
要求目标 Unit 已经处于 active,否则当前 Unit 启动失败。
所属 Section
[Unit]
语法
Requisite=<UNIT> [...]推荐示例
Requisite=network.target关键边界
它不会主动启动目标 Unit。
BindsTo=<UNIT> [...]比 Requires 更强,把当前 Unit 生命周期绑定到目标 Unit。
BindsTo=
比 Requires 更强,把当前 Unit 生命周期绑定到目标 Unit。
所属 Section
[Unit]
语法
BindsTo=<UNIT> [...]推荐示例
BindsTo=dev-sdb.device关键边界
目标消失或 inactive 时当前 Unit 也会被停止,适合设备等紧耦合关系。
PartOf=<UNIT> [...]把目标 Unit 的 stop/restart 单向传播给当前 Unit。
PartOf=
把目标 Unit 的 stop/restart 单向传播给当前 Unit。
所属 Section
[Unit]
语法
PartOf=<UNIT> [...]推荐示例
PartOf=app.target关键边界
启动不会自动传播;常用于一组服务随父级一起重启。
Conflicts=<UNIT> [...]声明两个 Unit 不能同时 active。
Conflicts=
声明两个 Unit 不能同时 active。
所属 Section
[Unit]
语法
Conflicts=<UNIT> [...]推荐示例
Conflicts=legacy-app.service关键边界
启动一方会导致另一方被停止;通常再结合 ordering。
DefaultDependencies=yes|no控制 systemd 是否自动补充此 Unit 类型的默认依赖。
DefaultDependencies=
控制 systemd 是否自动补充此 Unit 类型的默认依赖。
所属 Section
[Unit]
语法
DefaultDependencies=yes|no推荐示例
DefaultDependencies=no关键边界
绝大多数普通服务保持默认 yes;关闭后必须自己理解并补足启动/关闭顺序。
[Unit] 顺序、绑定与失败传播
After=<UNIT> [...]配置启动顺序:当前 Unit 在指定 Unit 之后启动。
After=
配置启动顺序:当前 Unit 在指定 Unit 之后启动。
所属 Section
[Unit]
语法
After=<UNIT> [...]推荐示例
After=network-online.target关键边界
只影响 ordering,不会自动把对方加入启动事务。
Before=<UNIT> [...]配置启动顺序:当前 Unit 在指定 Unit 之前启动。
Before=
配置启动顺序:当前 Unit 在指定 Unit 之前启动。
所属 Section
[Unit]
语法
Before=<UNIT> [...]推荐示例
Before=nginx.service关键边界
与 After 相反,同样不自动创建依赖。
Wants=<UNIT>
After=<UNIT>常见组合:弱依赖并等待目标先完成启动。
Wants= + After=
常见组合:弱依赖并等待目标先完成启动。
所属 Section
[Unit]
语法
Wants=<UNIT>
After=<UNIT>推荐示例
Wants=network-online.target
After=network-online.target关键边界
很多“依赖某服务先准备好”的场景需要两个维度同时表达。
Requires=<UNIT>
After=<UNIT>强依赖并要求目标先启动。
Requires= + After=
强依赖并要求目标先启动。
所属 Section
[Unit]
语法
Requires=<UNIT>
After=<UNIT>推荐示例
Requires=postgresql.service
After=postgresql.service关键边界
如果数据库只是外部依赖,强绑定不一定合适;先确认是否真的需要 Requires。
OnFailure=<UNIT> [...]当前 Unit 进入 failed 后启动指定 Unit。
OnFailure=
当前 Unit 进入 failed 后启动指定 Unit。
所属 Section
[Unit]
语法
OnFailure=<UNIT> [...]推荐示例
OnFailure=notify-admin@%n.service关键边界
适合故障通知或恢复动作;注意避免故障处理 Unit 自身形成循环。
OnSuccess=<UNIT> [...]Unit 成功进入 inactive 后触发指定 Unit。
OnSuccess=
Unit 成功进入 inactive 后触发指定 Unit。
所属 Section
[Unit]
语法
OnSuccess=<UNIT> [...]推荐示例
OnSuccess=cleanup.service关键边界
较新 systemd 才支持,跨老发行版前先核对版本。
PropagatesReloadTo=<UNIT> [...]把当前 Unit 的 reload 请求传播给指定 Unit。
PropagatesReloadTo=
把当前 Unit 的 reload 请求传播给指定 Unit。
所属 Section
[Unit]
语法
PropagatesReloadTo=<UNIT> [...]推荐示例
PropagatesReloadTo=app-worker.service关键边界
只传播 reload,不代表依赖或排序。
JoinsNamespaceOf=<UNIT> [...]让当前 Unit 加入其他 Unit 创建的部分命名空间。
JoinsNamespaceOf=
让当前 Unit 加入其他 Unit 创建的部分命名空间。
所属 Section
[Unit]
语法
JoinsNamespaceOf=<UNIT> [...]推荐示例
JoinsNamespaceOf=app.service关键边界
属于高级隔离场景;普通业务服务通常无需配置。
[Unit] 条件与断言
ConditionPathExists=<PATH>只有路径存在时才执行 Unit。
ConditionPathExists=
只有路径存在时才执行 Unit。
所属 Section
[Unit]
语法
ConditionPathExists=<PATH>推荐示例
ConditionPathExists=/etc/app/config.yml关键边界
Condition 不满足时通常是跳过而非 failed。
ConditionPathIsDirectory=<PATH>要求目标路径存在且为目录。
ConditionPathIsDirectory=
要求目标路径存在且为目录。
所属 Section
[Unit]
语法
ConditionPathIsDirectory=<PATH>推荐示例
ConditionPathIsDirectory=/srv/app关键边界
适合依赖挂载目录/数据目录的服务。
ConditionFileNotEmpty=<PATH>要求文件存在且非空。
ConditionFileNotEmpty=
要求文件存在且非空。
所属 Section
[Unit]
语法
ConditionFileNotEmpty=<PATH>推荐示例
ConditionFileNotEmpty=/etc/app/token关键边界
可用于启动前的最低配置完整性判断。