[Solved] How to start an app “detached” in separate process. The started process must run in its own session and act like a daemon


on linux go version 1.15.2, i run below code and it spawns a new process that does not die with main.

package main

import (
    "log"
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("go", "run", "./d")
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    log.Printf("Running command and waiting for it to finish...")
    err := cmd.Start()
    if err != nil {
        log.Fatal("cmd.Start failed: ", err)
    }
    err = cmd.Process.Release()
    if err != nil {
        log.Fatal("cmd.Process.Release failed: ", err)
    }
}

where ./d/main.go is

package main

import (
    "fmt"
    "time"
)

func main() {
    go func() {
        for {
            fmt.Println("hop")
            <-time.After(time.Second)
        }
    }()
    <-make(chan bool)
}

1

solved How to start an app “detached” in separate process. The started process must run in its own session and act like a daemon