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

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

go run app.go always run in the background

问题

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

我的server.go文件如下:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func handler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, "Hi there X, I love %s!", r.URL.Path[1:])
  8. }
  9. func main() {
  10. http.HandleFunc("/", handler)
  11. http.ListenAndServe(":8080", nil)
  12. }

然后我运行:go run server.go

VS Code的Launch.json配置如下:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Launch",
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${workspaceRoot}",
  13. "env": {},
  14. "args": [],
  15. "showLog": true
  16. }
  17. ]
  18. }

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

  1. 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}
  2. 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:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func handler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, "Hi there X, I love %s!", r.URL.Path[1:])
  8. }
  9. func main() {
  10. http.HandleFunc("/", handler)
  11. http.ListenAndServe(":8080", nil)
  12. }

And I run: go run server.go

VS Code Launch.json:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Launch",
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${workspaceRoot}",
  13. "env": {},
  14. "args": [],
  15. "showLog": true
  16. }
  17. ]
  18. }

Update: I get this error in VS Code Terminal:

  1. 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}
  2. 2017/04/07 16:41:41 debugger.go:412: continuing

答案1

得分: 11

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

  1. nohup go run server.go &
英文:

Did you tried running your server using nohup?

  1. 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:

确定