跳转至

Linux systemd 服务编排入门

systemd 不只是“启动服务”,还负责依赖、资源限制、日志与进程生命周期管理。

1. 一个最小的 service

/etc/systemd/system/myapp.service

[Unit]
Description=My App
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/myapp
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

2. 常用命令

systemctl daemon-reload
systemctl enable --now myapp
systemctl status myapp
journalctl -u myapp -f

3. 常见失败点

  • ExecStart 路径不对或无执行权限
  • 环境变量缺失(用 Environment=EnvironmentFile=
  • 端口占用或依赖服务未就绪(用 After=/Wants=