# Linux 如何写一个 systemd 服务

URL: https://caijiao.org/how-to/linux-docker/linux-systemd-unit
Source: docs/how-to/linux-docker/linux-systemd-unit.md
Description: Linux 如何写一个 systemd 服务：创建 .service 文件，配置 ExecStart、WorkingDirectory 和 Restart。 包含可执行命令、排查顺序和相关 Linux、Docker 内容入口。

## 快速答案

创建 .service 文件，配置 ExecStart、WorkingDirectory 和 Restart。

```bash
sudo tee /etc/systemd/system/my-app.service
sudo systemctl daemon-reload
sudo systemctl enable --now my-app
```

## 使用要点

- 先看服务状态和最近日志，再决定 reload、restart 或结束进程。
- 生产环境避免直接使用强制结束命令，除非已经确认进程无法正常退出。

## 相关内容

- [Linux / Docker 问题](/how-to/linux-docker/)
- [Linux 教程](/linux/)
- [Docker 教程](/docker/)
- [Linux 网络工具](/linux/network/tools)
- [Linux 日志管理](/linux/system/log-management)
