> For AI agents: the complete documentation index is available at https://www.tteam.icu/zh/llms.txt, the full documentation bundle is available at https://www.tteam.icu/zh/llms-full.txt.

# linux用systemd运行二进制


AI 总结AI 生成

当 VPS 只有 `1 核 512 MB` 时，为了运行一个 Golang 演示服务再装 Docker，成本其实不低。这里选择让 `systemd` 直接托管二进制。
| 环节 | 需要做             |
| -- | --------------- |
| 准备 | 上传二进制并赋予执行权限    |
| 托管 | 编写 unit 文件并启用服务 |
| 更新 | 替换二进制后重启服务      |
方案没有镜像层和容器运行时开销，适合这类低配机器上的单文件服务。

## 前言

最近购买了一台VPS，配置为1核512M内存，系统为Debian

我计划在这台VPS上运行我用`Golang`开发的项目演示站

在其他服务器上我通常使用Docker运行应用，但由于这台服务器的CPU和内存资源有限，我决定直接使用系统的systemd来管理运行

## 开始

先将二进制文件上传到服务器并赋予执行权限

然后写入`systemd`文件，并运行即可

```bash
cat << EOF > /etc/systemd/system/hertz_service.service
[Unit]
Description=hertz_service

[Service]
ExecStart=/root/hertz_service --config=config.yaml
WorkingDirectory=/root/
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=hertz_service
User=root

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable hertz_service --now
```

## 更新服务

```bash
#!/bin/bash

app_full_path=/root/hertz_service
download_url=https://xxx/hertz_service

rm -f ${app_full_path}
wget ${download_url} -O ${app_full_path}
chmod +x ${app_full_path}
systemctl restart hertz_service
```
