duanjianxu4288 2016-05-18 11:04
浏览 220

为什么systemd无法启动golang Web应用程序,没有答案

My golang web app cannot start when I use systemd, but it works fine when manually start. What are problems with my systemd configuration?

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.target

backend is a binary file compiled with command: env GOOS=linux GOARCH=amd64 go build -v bitbucket.org/myapp/backend

systemd service status

$ 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: It looks like my web app started, but then stoped immediately.

I try config Type=forking, then service status show as below. Could someone explains why the log Started Backend service. and Starting Backend service... order is reversed.

$ 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.

Result when I run web app manually (from terminal):

$ /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

Update:

  1. I try supervisord and it works fine (same binary file)
  2. With systemd, I changed service config to Restart=always, RestartSec=15. And systemd keeps restarting my web app.

Does anyone know why supervisord work fine but systemd not? I think that systemd would work fine with a basic task like that!

  • 写回答

4条回答 默认 最新

  • douhong4452 2016-05-18 11:51
    关注

    I'll take a wild stab and guess your app listens on port 80 and/or 443, so your best bet is to use setcap on it to give it permission.

    example: sudo setcap 'cap_net_bind_service=+ep' /u01/backend, this will need to be done every time you compile your app.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作