当我使用systemd时,我的golang web应用程序无法启动,但手动启动时它可以正常工作。我的系统配置有什么问题?
goweb.service
$ cat goweb.service
[Unit]
Description=Backend service
After=network.target
[Service]
User=myapp
Group=myapp
Restart=on-failure
ExecStart=/u01/backend
[Install]
WantedBy=multi-user.targetbackend是使用命令:env GOOS=linux GOARCH=amd64 go build -v bitbucket.org/myapp/backend编译的二进制文件。
系统服务状态
$ sudo service goweb status
Redirecting to /bin/systemctl status goweb.service
● goweb.service - Backend service
Loaded: loaded (/usr/lib/systemd/system/goweb.service; disabled; vendor preset: disabled)
Active: inactive (dead)
May 18 10:55:56 instance-1 systemd[1]: Started Backend service.
May 18 10:55:56 instance-1 systemd[1]: Starting Backend service...P/S:看上去我的网络应用程序启动了,但很快就停止了。
我尝试配置Type=forking,然后显示服务状态,如下所示。有人能解释为什么日志Started Backend service.和Starting Backend service...顺序被颠倒了吗?
$ sudo service goweb status
Redirecting to /bin/systemctl status goweb.service
● goweb.service - Backend service
Loaded: loaded (/usr/lib/systemd/system/goweb.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2016-05-18 11:06:02 UTC; 2s ago
Process: 25847 ExecStart=/u01/backend (code=exited, status=0/SUCCESS)
May 18 11:06:02 instance-1 systemd[1]: Starting Backend service...
May 18 11:06:02 instance-1 systemd[1]: Started Backend service.结果当我手动运行web应用程序时(从终端):
$ /u01/backend
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] POST /upload --> main.uploadFileHandler (3 handlers)
[GIN-debug] Environment variable PORT="9005"
[GIN-debug] Listening and serving HTTP on :9005更新
systemd,我将服务配置更改为Restart=always,RestartSec=15。systemd继续重启我的网络应用程序。有人知道为什么supervisord工作得很好,但systemd却不行吗?我认为systemd可以很好地完成这样的基本任务!
发布于 2016-05-18 11:51:12
我会疯狂地尝试一下,猜你的应用程序会在端口80和/或443上监听,所以你最好的选择是在上面使用setcap来允许它。
示例:sudo setcap 'cap_net_bind_service=+ep' /u01/backend,每次编译应用程序时都需要这样做。
发布于 2017-04-12 20:06:08
我知道这是旧的,但看到没有足够的答案,所以我想我会张贴。我的应用程序也是手动运行的,但是当使用systemd时,它就不能工作了。我意识到这与Go程序将声明的文件有关,例如:
tpl = template.Must(template.ParseGlob("templates/*"))在手动运行应用程序时,模板文件夹相对于我的main.go文件是存在的,但是当systemd运行它时,文件路径是不同的(还不确定为什么或者如何解决这个问题),但是现在我只是硬编码了一个绝对文件,例如:
tpl = template.Must(template.ParseGlob("./home/ubuntu/templates/*"))现在系统开始工作了。
希望这能有所帮助。如果有人能详细说明如何想出一个更好的解决方案,也会对我有帮助的!
发布于 2018-11-19 14:38:13
从上面的@RijulSudhir注释,插入:
WorkingDirectory=/home/ubuntu/如下所示:
[service]系统服务文件的。
https://stackoverflow.com/questions/37297714
复制相似问题