【golang】单机部署systemctl方案

概要

本文简单记录go编译后的程序,单机部署systemctl方案,开机启动等问题。

新增service文件

在/etc/systemd/system/目录下新增 testapi.service

[Unit]
Description=myGoApp

[Service]
User=root
Group=root
Type=simple
WorkingDirectory=/home/www/projectName/
ExecStart=/home/www/projectName/testapi serve
Restart=on-failure
RestartSec=20s
LimitNOFILE=infinity


[Install]
WantedBy=multi-user.target

提示:/home/www/projectName/testapi serve,根据具体服务情况来填,我这里需要传递serve参数

参数Restart 可选值还有:always。还有一个参数ExceStartPre,大家可以自行查阅相关信息。

配置文件添加完后,常用的命令有:

systemctl daemon-reload // 重载配置
systemctl enable testapi  //开机启动
systemctl start testapi  //启动服务
systemctl stop testapi  //停止服务

编辑testapi.service文件以后:Warning: vrapi.service changed on disk. Run 'systemctl daemon-reload' to reload units.

技术细节

如果发现服务无法启动,可以查看日志

tail -f -n 100 /var/log/messages

Jan 12 16:01:32 localhost systemd: Started myGoApp.
Jan 12 16:01:32 localhost systemd: Starting myGoApp...
Jan 12 16:01:32 localhost vrapi: 2024/01/12 16:01:32 config.Setup, fail to parse file conf/app.ini: open conf/app.ini: no such file or directory
Jan 12 16:01:32 localhost systemd: testapi.service: main process exited, code=exited, status=1/FAILURE
Jan 12 16:01:32 localhost systemd: Unit testapi.service entered failed state.
Jan 12 16:01:32 localhost systemd: testapi.service failed.
Jan 12 16:01:33 localhost systemd: testapi.service holdoff time over, scheduling restart[Unit]

提示:我这里的错误是因为之前没有指定:WorkingDirectory,导致go程序的配置文件无法加载。

小结

这就是一个简单的单机部署开机启动、报错重启等方案了。此方案还有很多不足,如无法实现优雅的重启等,只是简单粗暴的保持了服务的可用。欢迎留言指正,共同学习。

参考

golang是如何部署到服务器的

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小镇学者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值