0

I'm trying to figure out how to run an executable file from Go using ForkExec. Having tried this so far:

var sysProcAttr = syscall.SysProcAttr{}
sysProcAttr.Setsid = true
sysProcAttr.Foreground = false
var procAttr = syscall.ProcAttr{
    Dir: "/",
    Env: nil,
    Sys: &sysProcAttr,
}

processID, err = syscall.ForkExec("/home/ubuntu/.dotnet/dotnet", []string{"/home/ubuntu/my-executable-net-dll-file"}, &procAttr)
return processID, err

This creates a process, but when I close the Go application these processes close as well. The question is - how do I detach the process from it? It appears that Setsid doesn't work, the only options left are to use nohup or "bash -c", but I fear the results will be the same.

Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39
  • 1
    i think this question is answered already many times - https://stackoverflow.com/search?q=golang+detach+process THere - https://stackoverflow.com/questions/59410139/run-command-in-golang-and-detach-it-from-process – vodolaz095 Nov 08 '21 at 13:18
  • @vodolaz095 There were suggestions on how to achieve desired result, but no answers. Overall I understand it that every process must have a parent. If that's the case, then I need to make a dummy, very simple, infallible process, and let it spawn the necessary processes. My only concern for detaching the process is that current one may crash, destroying all the children. – Daniel Protopopov Nov 08 '21 at 14:28
  • @DanielProtopopov: a crashing process does not destroy all children. I think there's some other misunderstanding going on here that is causing your problem. Things like ensuring the process is in a separate process group, or simply [releasing]https://stackoverflow.com/a/65234924/32880) the child process is usually all that's needed. – JimB Nov 08 '21 at 14:35
  • @JimB alright, the process is stopped (not crashing) by the IDE, yet it stops all of its children processes. You may be right about the crash, but nevertheless I would like to avoid stopping of the processes either way its crashed or stopped normally. I've found the way to do this through [daemonization](http://software.clapper.org/daemonize/). – Daniel Protopopov Nov 08 '21 at 14:45
  • 1
    @DanielProtopopov, that is something done by the IDE itself then, not a property of the process or OS. That behavior usually relies on signaling the process group, you trying setting the pgid? https://stackoverflow.com/questions/33165530/prevent-ctrlc-from-interrupting-exec-command-in-golang/33171307#33171307 – JimB Nov 08 '21 at 14:47
  • @JimB Well, I'll be damned, that worked! I used Setsid which didn't work, but Setpgid worked just fine! Many thanks! – Daniel Protopopov Nov 08 '21 at 15:00

1 Answers1

1

when I close the Go application these processes close as well.

os/exec's Command doesn't seem to have the same behavior, at least not inherently:

% pid=$(go run t.go); while ps $pid; do sleep 1; done
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
[2021/11/08 08:23:45 CST ] ~
package main

import (
    "os/exec"
    "fmt"
)

func main() {
    c := exec.Command("bash", "-c", "sleep 3; echo slept 3; sleep 3; echo slept 3 more")
    if err := c.Start(); err != nil {
        panic(err)
    }
    fmt.Println(c.Process.Pid)
}

It could depend on spawned process behavior. See https://unix.stackexchange.com/questions/158727/is-there-any-unix-variant-on-which-a-child-process-dies-with-its-parent

erik258
  • 14,701
  • 2
  • 25
  • 31