systemd 常用命令速查
常用命令
systemctl status app
systemctl start app
systemctl stop app
systemctl restart app
systemctl reload app
systemctl enable app
systemctl disable app
systemctl enable --now app
systemctl is-active app
systemctl is-enabled app
systemctl daemon-reload
systemctl list-units --failed
systemctl list-unit-files
systemctl cat app
systemctl edit app
systemctl show app
journalctl -u app -f
journalctl -u app --since '1 hour ago'
journalctl -p err -b
systemd-analyze blame
systemd-analyze critical-chain
场景速查
| 目的 |
命令 |
| 查看依赖 |
systemctl list-dependencies app |
| 查看定时器 |
systemctl list-timers |
| 查看 target |
systemctl get-default / set-default |
| 重载单个 unit |
systemctl reload-or-restart app |
| 临时屏蔽 |
systemctl mask app / unmask app |
| 查看日志占用 |
journalctl --disk-usage |
| 清理日志 |
journalctl --vacuum-size=500M |
| 启动耗时 |
systemd-analyze blame |
易错点
- 修改 unit 后必须
daemon-reload,否则仍用旧配置。
enable 不等于 start;只 start 不会开机自启。
Type=simple/forking/notify 选错会导致状态误判。
ExecStart 要用绝对路径,环境变量和 WorkingDirectory 显式配置。
Restart=always 可能掩盖启动失败;先看日志再决定。
journalctl 的持久化取决于 Storage= 配置,重启后可能丢失。
- 容器里通常不要让 systemd 当 PID 1;应用应前台运行。
相关命令
systemctl — 完整命令页
journalctl — 日志查询
systemd-analyze — 启动分析