退出后保持外部进程运行 更新

问题描述

如何在主退出后保持进程运行?

更新:事实证明,只有当您在 goland 中运行时才会出现这种情况。包括评论在内的接受答案解决了这个问题。

信息:我有一个可执行文件可以监视文件夹中的更改,我需要从 go 启动它并在退出后保持运行。

我看到了 this 但它没有解决退出后运行进程的问题。

package main

import "os/exec"

func main() {
    cmd := exec.Command("sh","long_running process","&")
    cmd.Start()
}
fmt.Println("Sleeping...")
time.Sleep(8 * time.Second) // I can see the process running

之后,当我执行“ps”时,进程被主应用程序杀死。

解决方法

我无法重现您遇到的问题。当我运行 sleep 命令并且 goroutine 终止时,当我使用 ps

搜索它时它仍在运行

更新

  1. 无法使用 GoLand 中的调试器运行它。
  2. 除非,如果您在调试选项窗口中启用作为 sudo 运行。
  3. 不带 sudo:带 go run,或带 dlv debug不带 GoLand 中的调试器。
package main

import (
    "os/exec"
)

func main() {
    cmd := exec.Command("sleep","99999999")
    cmd.Start()
}
~/tempgo/process
▶ go run process.go

~/tempgo/process
▶ ps -ax | grep "sleep"
29907 ttys002    0:00.00 sleep 99999999
29925 ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox sleep

~/tempgo/process