在Ubuntu上作为systemctl服务运行时Go找不到文件

时间:2019-04-20 09:29:09

标签: go systemctl

我有一个Go应用,正在尝试将其作为systemctl服务(Ubuntu 18.04)运行。

我正在使用godotenv

func init() {
    var env map[string]string
    env, err := godotenv.Read()
    if err != nil {
        panic(err)
    }
}

我的.env文件位于可执行文件所在的目录中。

我已经创建了一个service文件:

[Unit]
Description=my go app
Requires=local-fs.target
After=rsyslog.service

[Service]
Type=forking
GuessMainPID=no
StandardInput=null
ExecStart=/var/path/to/my/app/main

[Install]
WantedBy=default.target

先执行sudo systemctl start my-go-app.service,然后执行sudo systemctl status my-go-app.service,然后将这些记录在日志中:

  

正在启动我的应用......
  紧急:打开.env:没有此类文件或目录

怎么了?

直接执行main时,没有此类问题。

1 个答案:

答案 0 :(得分:5)

  

我的.env文件位于可执行文件所在的目录中。

然后,您需要配置工作目录以匹配:

[Service]
...
WorkingDirectory=/var/path/to/my/app

在更改任何单位文件后,请记住要运行systemctl daemon-reload