运行`go run app.go`会一直在后台运行。

huangapple go评论78阅读模式
英文:

go run app.go always run in the background

问题

我有一个简单的HTTP服务器,并且我想将其作为后台进程运行。

我的server.go文件如下:

package main
    
import (
    "fmt"
    "net/http"
)
    
func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there X, I love %s!", r.URL.Path[1:])
}
    
func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

然后我运行:go run server.go

VS Code的Launch.json配置如下:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}",
      "env": {},
      "args": [],
      "showLog": true
    }
  ]
}

更新:我在VS Code终端中遇到了以下错误:

2017/04/07 16:41:41 debugger.go:257: created breakpoint: &api.Breakpoint{ID:1, Name:"", Addr:0x12063ef, File:"/Users/X/Documents/X/play/go/server.go", Line:9, FunctionName:"main.handler", Cond:"", Tracepoint:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(nil), LoadLocals:(*api.LoadConfig)(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0}
2017/04/07 16:41:41 debugger.go:412: continuing
英文:

I have a simple HTTP server, and I want to run it as a process in the background.

my server.go file is like:

package main
    
import (
    "fmt"
    "net/http"
)
    
func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there X, I love %s!", r.URL.Path[1:])
}
    
func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

And I run: go run server.go

VS Code Launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}",
      "env": {},
      "args": [],
      "showLog": true
    }
  ]
}

Update: I get this error in VS Code Terminal:

2017/04/07 16:41:41 debugger.go:257: created breakpoint: &api.Breakpoint{ID:1, Name:"", Addr:0x12063ef, File:"/Users/X/Documents/X/play/go/server.go", Line:9, FunctionName:"main.handler", Cond:"", Tracepoint:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(nil), LoadLocals:(*api.LoadConfig)(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0}
2017/04/07 16:41:41 debugger.go:412: continuing

答案1

得分: 11

你尝试过使用nohup来运行你的服务器吗?

nohup go run server.go &
英文:

Did you tried running your server using nohup?

nohup go run server.go &

huangapple
  • 本文由 发表于 2017年4月7日 22:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/43280597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定