Linux 与服务器运维 / 知识点
systemd / systemctl 命令速查手册
服务生命周期
systemctl reload <UNIT>请求服务进程重读自身配置。
重载服务自身配置
请求服务进程重读自身配置。
基本语法
systemctl reload <UNIT>推荐用法
sudo systemctl reload nginx.service预期结果
支持 reload 时服务无重启完成配置刷新。关键边界
与 daemon-reload 完全不同。
systemctl reload-or-restart <UNIT>优先 reload,不支持则 restart。
Reload 不支持则 Restart
优先 reload,不支持则 restart。
基本语法
systemctl reload-or-restart <UNIT>推荐用法
sudo systemctl reload-or-restart nginx.service预期结果
完成 reload 或 restart。关键边界
脚本使用方便,但必须接受可能重启带来的中断。
systemctl try-restart <UNIT>只有 active Unit 才重启。
仅运行中才 Restart
只有 active Unit 才重启。
基本语法
systemctl try-restart <UNIT>推荐用法
sudo systemctl try-restart nginx.service预期结果
active 时重启,inactive 保持不变。关键边界
适合升级脚本避免把原本关闭的服务启动。
systemctl restart <UNIT1> <UNIT2> ...一次操作多个 Unit。
批量重启多个 Unit
一次操作多个 Unit。
基本语法
systemctl restart <UNIT1> <UNIT2> ...推荐用法
sudo systemctl restart nginx.service app.service预期结果
systemd 为多个 Unit 创建 jobs。关键边界
任一失败会影响总体退出码。
systemctl --no-block start <UNIT>提交 start job 后立即返回。
异步提交启动 Job
提交 start job 后立即返回。
基本语法
systemctl --no-block start <UNIT>推荐用法
sudo systemctl --no-block start app.service预期结果
命令不等待 activation 完成。关键边界
返回 0 只代表 job 成功提交,不代表服务已 ready。
开机启用、禁用与屏蔽
systemctl enable <UNIT>创建 Unit 安装关系。
设置开机启用
创建 Unit 安装关系。
基本语法
systemctl enable <UNIT>推荐用法
sudo systemctl enable nginx.service预期结果
通常输出创建的 symlink。关键边界
enable 默认不会立即启动服务。
systemctl enable --now <UNIT>同时 enable 并 start。
启用并立即启动
同时 enable 并 start。
基本语法
systemctl enable --now <UNIT>推荐用法
sudo systemctl enable --now nginx.service预期结果
Unit enabled 且 active。关键边界
部署新服务最常用。
systemctl disable <UNIT>删除 enable 创建的安装关系。
取消开机启用
删除 enable 创建的安装关系。
基本语法
systemctl disable <UNIT>推荐用法
sudo systemctl disable nginx.service预期结果
相关 symlink 被移除。关键边界
disable 默认不会停止当前正在运行的服务。
systemctl disable --now <UNIT>同时 disable 并 stop。
禁用并立即停止
同时 disable 并 stop。
基本语法
systemctl disable --now <UNIT>推荐用法
sudo systemctl disable --now nginx.service预期结果
服务停止且不再按原关系自动启动。关键边界
生产环境执行前确认依赖。
systemctl reenable <UNIT>先 disable 再 enable,按当前 [Install] 重建链接。
重新建立启用关系
先 disable 再 enable,按当前 [Install] 重建链接。
基本语法
systemctl reenable <UNIT>推荐用法
sudo systemctl reenable app.service预期结果
安装关系重新生成。关键边界
Unit 安装配置发生变化后有用。
systemctl preset <UNIT>按照发行版 preset policy 启用或禁用 Unit。
按 Preset 策略应用状态
按照发行版 preset policy 启用或禁用 Unit。
基本语法
systemctl preset <UNIT>推荐用法
sudo systemctl preset nginx.service预期结果
Unit state 按 preset 调整。关键边界
preset 是发行版/镜像策略,不等于简单 enable。
systemctl mask <UNIT>阻止 Unit 被正常启动。
屏蔽 Unit
阻止 Unit 被正常启动。
基本语法
systemctl mask <UNIT>推荐用法
sudo systemctl mask nginx.service预期结果
Unit 变为 masked。关键边界
比 disable 更强,依赖方启动它也会失败。
systemctl mask --now <UNIT>停止服务并阻止后续启动。
屏蔽并立即停止
停止服务并阻止后续启动。
基本语法
systemctl mask --now <UNIT>推荐用法
sudo systemctl mask --now nginx.service预期结果
Unit stopped 且 masked。关键边界
可能影响依赖服务。
systemctl unmask <UNIT>移除 mask。
解除 Unit 屏蔽
移除 mask。
基本语法
systemctl unmask <UNIT>推荐用法
sudo systemctl unmask nginx.service预期结果
Unit 恢复可启动状态。关键边界
不会自动 enable 或 start。
systemctl is-enabled <UNIT>脚本化检查 Unit File 状态。
判断是否启用
脚本化检查 Unit File 状态。
基本语法
systemctl is-enabled <UNIT>推荐用法
systemctl is-enabled nginx.service预期结果
输出 enabled、disabled、static、masked 等。关键边界
不要用 grep list-unit-files 替代。
配置、属性与来源检查
systemctl is-active <UNIT>检查当前运行状态。
判断是否 Active
检查当前运行状态。
基本语法
systemctl is-active <UNIT>推荐用法
systemctl is-active nginx.service预期结果
active 时输出 `active` 并通常返回 0。关键边界
比解析 status 文本稳定。
systemctl is-failed <UNIT>检查 Unit 是否 failed。
判断是否 Failed
检查 Unit 是否 failed。
基本语法
systemctl is-failed <UNIT>推荐用法
systemctl is-failed nginx.service预期结果
失败时输出 `failed`。关键边界
inactive 与 failed 是不同状态。
systemctl show <UNIT>读取 Unit 的 D-Bus 属性。
查看 Unit 全部属性
读取 Unit 的 D-Bus 属性。
基本语法
systemctl show <UNIT>推荐用法
systemctl show nginx.service预期结果
输出大量 KEY=VALUE。关键边界
脚本与深度排障比 status 更适合。
systemctl show -p <PROPERTY> <UNIT>只读取需要的属性。
读取指定属性
只读取需要的属性。
基本语法
systemctl show -p <PROPERTY> <UNIT>推荐用法
systemctl show -p MainPID -p ActiveState -p SubState nginx.service预期结果
输出指定字段。关键边界
自动化应尽量缩小字段集合。
systemctl show -p <PROPERTY> --value <UNIT>省略属性名,仅输出 value。
只输出属性值
省略属性名,仅输出 value。
基本语法
systemctl show -p <PROPERTY> --value <UNIT>推荐用法
systemctl show -p MainPID --value nginx.service预期结果
例如输出主进程 PID。关键边界
适合 Shell 命令替换。