linux用systemd运行二进制

前言

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

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

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

开始

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

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

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

更新服务

#!/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