> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 systemd 将 Bun 作为守护进程运行

[systemd](https://systemd.io) 是 Linux 操作系统的 init 系统和服务管理器，用于管理系统进程和服务的启动及控制。

***

要使用 **systemd** 将 Bun 应用作为守护进程运行，你需要在 `/lib/systemd/system/` 目录下创建一个 *服务文件*。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
cd /lib/systemd/system
touch my-app.service
```

***

下面是一个典型的服务文件示例，用于在系统启动时运行一个应用。你可以用它作为自己服务的模板。将 `YOUR_USER` 替换为你希望运行应用的用户名。若需以 `root` 用户运行，则将 `YOUR_USER` 替换为 `root`，不过基于安全考虑一般不建议这样做。

更多各项设置的说明请参考 [systemd 文档](https://www.freedesktop.org/software/systemd/man/systemd.service.html)。

```ini my-app.service icon="file-code" theme={"theme":{"light":"github-light","dark":"dracula"}}
[Unit]
# 描述该应用
Description=My App
# 在网络可用后启动该应用
After=network.target

[Service]
# 通常使用 'simple'
# 参见 https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
Type=simple
# 启动应用使用的用户
User=YOUR_USER
# 应用程序根目录路径
WorkingDirectory=/home/YOUR_USER/path/to/my-app
# 启动应用的命令
# 需使用绝对路径
ExecStart=/home/YOUR_USER/.bun/bin/bun run index.ts
# 重启策略
# 可选值为 {no|on-success|on-failure|on-abnormal|on-watchdog|on-abort|always}
Restart=always

[Install]
# 自动启动该应用
WantedBy=multi-user.target
```

***

如果你的应用启动了一个 Web 服务器，请注意普通非 `root` 用户默认无法监听 80 或 443 端口。若希望允许非 `root` 用户执行的 Bun 永久监听这些端口，可以使用以下命令。以 `root` 身份运行时不需要此步骤。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
setcap CAP_NET_BIND_SERVICE=+eip ~/.bun/bin/bun
```

***

配置好服务文件后，你可以 *启用* 该服务。启用后服务将在系统重启时自动启动。此操作需 `sudo` 权限。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
systemctl enable my-app
```

***

如果不想重启即可启动服务，可以手动 *启动* 它。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
systemctl start my-app
```

***

通过 `systemctl status` 检查应用状态。如果应用成功启动，你将看到类似下面的信息：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
systemctl status my-app
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
● my-app.service - My App
     Loaded: loaded (/lib/systemd/system/my-app.service; enabled; preset: enabled)
     Active: active (running) since Thu 2023-10-12 11:34:08 UTC; 1h 8min ago
   Main PID: 309641 (bun)
      Tasks: 3 (limit: 503)
     Memory: 40.9M
        CPU: 1.093s
     CGroup: /system.slice/my-app.service
             └─309641 /home/YOUR_USER/.bun/bin/bun run /home/YOUR_USER/application/index.ts
```

***

更新服务时，编辑服务文件内容后，需要重新加载守护进程。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
systemctl daemon-reload
```

***

有关服务单元配置的完整指南，可以查看[此页面](https://www.freedesktop.org/software/systemd/man/systemd.service.html)。或者参考以下常用命令速查表：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
systemctl daemon-reload # 通知 systemd 有文件更改
systemctl enable my-app # 启用应用（允许自动启动）
systemctl disable my-app # 禁用应用（关闭自动启动）
systemctl start my-app # 启动已停止的应用
systemctl stop my-app # 停止应用
systemctl restart my-app # 重启应用
```
