systemctl
systemctl 是 systemd 的统一管理入口,用于控制、查询和调试 Linux 系统中的服务(unit)、启动流程和系统状态。
systemd 管理的一切都是 Unit
| 类型 | 后缀 | 说明 |
|---|---|---|
| Service | .service |
服务(最常用) |
| Socket | .socket |
套接字激活 |
| Target | .target |
启动阶段 / 逻辑分组 |
| Timer | .timer |
定时任务(替代 cron) |
| Mount | .mount |
挂载点 |
| Path | .path |
文件路径监控 |
| Device | .device |
设备 |
| Slice | .slice |
资源控制组 |
| Scope | .scope |
外部进程 |
┌────────────┐
│ systemctl │
└─────┬──────┘
│
┌────▼──────────────────────┐
systemd (PID 1)
├─ unit 生命周期管理
├─ 依赖关系 (After/Wants)
├─ 资源控制 (cgroups)
├─ 日志 (journald)
└───────────────────────────┘
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx # 不重启,重载配置
restart:进程重启reload:发送信号(如 HUP),不中断服务
systemctl enable nginx # 开机自启
systemctl disable nginx
systemctl is-enabled nginx
本质:创建 / 删除 符号链接
systemctl status nginx
你能看到:
- Active 状态
- Main PID
- 最近日志
- 失败原因(exit code)
systemctl list-units --type=service
systemctl list-units --state=failed
systemctl list-unit-files
| 命令 | 作用 |
|---|---|
| list-units | 当前加载 |
| list-unit-files | 磁盘上存在 |
systemctl get-default
常见:
| target | 说明 |
|---|---|
| multi-user.target | 命令行服务器 |
| graphical.target | 桌面 |
| rescue.target | 单用户 |
| emergency.target | 紧急模式 |
systemctl set-default multi-user.target
systemctl isolate rescue.target
/etc/systemd/system/ # 管理员自定义(最高优先级)
/run/systemd/system/ # 运行时生成
/usr/lib/systemd/system/ # 发行版提供
永远不要直接改
/usr/lib
systemctl edit nginx
生成:
/etc/systemd/system/nginx.service.d/override.conf
systemctl daemon-reload
| 命令 | 含义 |
|---|---|
| daemon-reload | 重新加载 unit 文件 |
| daemon-reexec | 重新执行 systemd(更重) |
systemctl status rpcbind.socket
systemctl start rpcbind.socket
特点:
- 服务 按需启动
- socket 先监听,service 后启动
systemctl list-timers
启用 timer:
systemctl enable --now backup.timer
journalctl -u nginx
journalctl -u nginx -f
journalctl -xe
| 现象 | 可能原因 |
|---|---|
| status=203/EXEC | ExecStart 路径错误 |
| status=1/FAILURE | 程序自身错误 |
| core-dump | 程序 panic / assert |
| Failed at step | 权限 / SELinux |
systemd-analyze verify nginx.service
systemd 是 cgroups 的统一入口
[Service]
MemoryMax=1G
CPUQuota=50%
TasksMax=200
查看:
systemctl status nginx
systemd-cgtop
-
systemctl 不直接管理容器
-
但可以:
- 管理
podmanunit - 管理
cri-o - 管理 kubelet
- 管理
例:
systemctl enable podman@mycontainer
- ❌ 直接修改
/usr/lib/systemd/system/*.service - ❌ 忘记
daemon-reload - ❌ 不区分
enable和start - ❌ 用 kill 管服务而不是 systemctl
systemctl 是 systemd 的遥控器,systemd 是 Linux 上“进程 + 启动 + 资源 + 依赖”的统一调度器。
## 服务
systemctl start|stop|restart|reload xxx
systemctl status xxx
## 开机自启
systemctl enable|disable xxx
## unit 管理
systemctl daemon-reload
systemctl edit xxx
## 启动级别
systemctl get-default
systemctl set-default multi-user.target
## 日志
journalctl -u xxx -f
## 排障
systemd-analyze blame
systemd-analyze critical-chain
--type 用于 按 Unit 类型过滤
systemctl list-units --type=TYPE
systemctl list-unit-files --type=TYPE
| type | unit 后缀 | 说明 |
|---|---|---|
| service | .service |
服务(最常用) |
| socket | .socket |
套接字激活 |
| target | .target |
启动目标 / 分组 |
| timer | .timer |
定时器(cron 替代) |
| mount | .mount |
挂载点 |
| automount | .automount |
自动挂载 |
| path | .path |
路径监控 |
| device | .device |
设备 |
| swap | .swap |
swap 分区 |
| slice | .slice |
cgroup 资源切片 |
| scope | .scope |
外部进程组 |
systemctl list-units --type=service
systemctl list-units --type=socket
systemctl list-units --type=timer
--state 用于 按运行状态过滤
systemctl list-units --state=STATE
| state | 含义 |
|---|---|
| active | 正在运行 |
| inactive | 未运行 |
| activating | 正在启动 |
| deactivating | 正在停止 |
| reloading | 正在 reload |
systemctl list-units --state=active
systemctl list-units --state=activating
| state | 含义 |
|---|---|
| failed | 启动或运行失败 |
systemctl list-units --state=failed
⚠️ 只用于 list-unit-files
systemctl list-unit-files --state=STATE
| state | 含义 |
|---|---|
| enabled | 已设置开机自启 |
| disabled | 未设置自启 |
| static | 不能单独启用(被依赖) |
| masked | 被完全屏蔽 |
| generated | systemd 自动生成 |
| transient | 临时 unit |
| indirect | 间接启用 |
| bad | unit 文件损坏 |
| state | 说明 |
|---|---|
| plugged | 设备已就绪(device) |
| mounted | 已挂载(mount) |
| waiting | 等待条件(path / automount) |
systemctl list-units --type=service --state=failed
systemctl list-units --state=activating
systemctl list-units --type=timer
systemctl list-timers
systemctl list-units --type=socket
systemctl list-unit-files --state=enabled
systemctl list-units --type=service --state=inactive
| 命令 | 看什么 |
|---|---|
| list-units | 当前 已加载 / 运行态 |
| list-unit-files | 磁盘上的 unit 文件 |
- socket / target 也可以是 active
- 不一定有进程
## 所有 unit 类型
systemctl list-units --type=service
systemctl list-units --type=socket
systemctl list-units --type=timer
systemctl list-units --type=mount
systemctl list-units --type=target
## 状态过滤
systemctl list-units --state=active
systemctl list-units --state=failed
systemctl list-units --state=activating
## unit 文件状态
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=masked
# 查看所有已启用的服务
systemctl list-unit-files --type=service --state=enabled
# 仅查看当前正在运行的服务
# 不一定包含所有“已启用”的服务
systemctl list-units --type=service --state=running
# 查看特定服务是否启用
systemctl is-enabled nginx
要判断 systemd 服务是否在运行,最常用和推荐的方法是使用 systemctl is-active <服务名称> 查看是否 active,或者用 systemctl status <服务名称> 获取详细信息,其中关键是查看 Active: 状态是 active (running)。脚本中常结合 if systemctl is-active --quiet <服务名称>; then ... fi 来判断并执行后续操作。
systemctl status <服务名称>: 查看服务详细状态。
- Active: active (running) 表示服务正在运行。
- Active: inactive (dead) 表示服务已停止。
- Active: failed 表示服务启动失败。
- Active: activating 或 deactivating 表示服务正在启动或停止中。
systemctl is-active <服务名称>: 仅返回服务的运行状态(active 或 inactive),适合在脚本中判断。
- –quiet 选项 (如 systemctl is-active –quiet) 则不输出,只返回退出码,更适合自动化脚本。
systemctl list-units –type=service –state=running: 列出所有正在运行的服务。
pgrep <服务名>/pidof <服务名>: (辅助方式)通过进程名或 PID 检查,不依赖 systemctl 状态,但 systemctl 检查更全面。
#!/bin/bash
SERVICE_NAME="nginx" # 替换成你的服务名
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo "服务 $SERVICE_NAME 正在运行。"
else
echo "服务 $SERVICE_NAME 未运行,正在尝试启动..."
sudo systemctl start "$SERVICE_NAME"
fi
选择 systemctl is-active 进行判断,在脚本中获取服务的活动状态,是判断 systemd 服务运行情况最简洁高效的方法。
--type决定“看哪类 unit”,--state决定“看什么状态”,两者组合就是 systemd 的核心过滤能力。