服务与 unit 文件

systemd 通过 unit 文件描述服务如何启动、依赖哪些资源以及失败后如何处理。自定义服务通常放在 /etc/systemd/system 目录。

常用管理命令

修改 unit 文件后先重新加载配置,再启动服务并设置开机自启。

sudo systemctl daemon-reload
sudo systemctl enable --now my-app
sudo systemctl status my-app
sudo systemctl restart my-app

查看服务日志

journalctl 可以按服务筛选日志。-f 用于持续跟踪新日志,适合排查启动失败和运行时异常。

journalctl -u my-app --since today
journalctl -u my-app -f