2

How do you keep a process running after main exit?

Update: It turns out it is only when you are running in goland. The accepted answer including comments solved this.

Info: I have an executable that watches a folder for changes, and I need to start it from go and keep it running after exit.

I have see this but it does not solve the problem of running the process after exit.

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

Afterwards when I do "ps" the process is killed with the main application.

Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • If you place a long `Time.Sleep` are you able to verify the long running process is running using ps? – Christian Mar 24 '21 at 11:46
  • Thanks, yes I can verify the process is running. – Chris G. Mar 24 '21 at 11:47
  • 1
    Goland is probably killing the process group. See https://stackoverflow.com/questions/35433741/in-golang-prevent-child-processes-to-receive-signals-from-calling-process/35435038#35435038 – JimB Mar 24 '21 at 13:01

1 Answers1

3

I can't recreate the issue you are having. When I run the sleep command and the goroutine terminates, it is still running when I search for it with ps

Update

  1. Doesn't work running it with the debugger in GoLand.
  2. Except if you enable run as sudo in the debug options window.
  3. Without sudo: either with go run, or with dlv debug or without the debugger in 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
s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
Christian
  • 1,676
  • 13
  • 19
  • Thanks but nope, the "sleep" application is gone with the exit of main. I will update my question using "sleep". – Chris G. Mar 24 '21 at 11:57
  • Are you running it via the debugger? (or via Goland or VS code?) – Christian Mar 24 '21 at 11:58
  • Bouth with and without the debugger attached in goland. Hmm good question, I will try outside goland... – Chris G. Mar 24 '21 at 11:59
  • It doesn't work in Goland... `go run...` from the terminal works. – Christian Mar 24 '21 at 11:59
  • Any explaination to why goland won't allow this to work? – aclowkay Mar 24 '21 at 12:28
  • @aclowkay that I am not sure about. When you run/debug it with delve via the terminal it works correctly. – Christian Mar 24 '21 at 12:40
  • @aclowkay This is the only other info I could find https://forum.golangbridge.org/t/trying-not-to-kill-linux-process-started-in-go/17535/5 – Christian Mar 24 '21 at 12:51
  • It works with the goland debugger AND run as sudo. Not an ideal solution though. – Christian Mar 24 '21 at 13:01
  • I cannot reproduce the issue with/without sudo and debugger inside GoLand (2021.1 Beta 3). What GoLand version are you using (`Help | About`)? – s0xzwasd Mar 25 '21 at 10:26
  • GoLand 2019.2.5 – Christian Mar 25 '21 at 10:28
  • Thanks. Please use the latest release version or try out our [Beta 3 version](https://www.jetbrains.com/go/nextversion/). We slightly change implementation in the newer versions and it should work as expected (spawn a process in the background) without sudo. – s0xzwasd Mar 25 '21 at 11:14