dongying3830
2019-04-20 09:29
浏览 534
已采纳

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

I have a Go app, and I'm trying to run it as a systemctl service (Ubuntu 18.04).

I'm using godotenv:

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

I have my .env file in the same directory where my executable is.

I've created a service file:

[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

After executing sudo systemctl start my-go-app.service, then sudo systemctl status my-go-app.service, I got these in my logs:

Starting my-go-app...
panic: open .env: no such file or directory

What's wrong?

While executing main directly, there are no such problems.

图片转代码服务由CSDN问答提供 功能建议

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

我正在使用 godotenv

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

我的 .env 文件与可执行文件位于同一目录中。

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

  [Unit] 
Description = my go app \  nRequires = local-fs.target 
在= rsyslog.service 
 
 [服务] 
类型=分叉
GuessMainPID =没有
StandardInput = NULL 
ExecStart =的/ var /路径/到/我的/应用/主
 \  n [Install] 
WantedBy = default.target 
   
 
 

执行 sudo systemctl后,启动my-go-app.service ,然后 sudo systemctl status my-go-app.service ,我将这些保存在日志中:

正在启动my-go-app。 。
紧急:打开.env:没有此类文件或目录

出什么问题了?

执行中 直接是 main ,就没有这种问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandao6414 2019-04-20 09:36
    已采纳

    I have my .env file in the same directory where my executable is.

    Then you need to configure your working directory to match:

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

    Remember to run systemctl daemon-reload after changing any unit file.

    点赞 评论

相关推荐 更多相似问题