go fork daemon to background

sometimes you just need a persistent server

SEAN K.H. LIAO

go fork daemon to background

sometimes you just need a persistent server

fork to background in Go

So, you want to execute a process, forking it to the background and keep it running after the parent process exits.

First.... try not to do this, mysterious persistent daemons is a management nightmare.

But if you need to do it:

package main

import (
        "fmt"
        "os/exec"
        "syscall"
)

func main() {
        cmd := exec.Command("./background-process")
        cmd.SysProcAttr = &syscall.SysProcAttr{
                Setsid: true,
        }
        cmd.Start()
        fmt.Println("run, exit")
}

This makes use of setsid, starting a new process group disassociated with the current tree. In Go, syscall.SysProcAttr.Setsid with a zero Pgid (parent group ID) starts a new group. And os/exec.Cmd.Start doesn't wait for the process to complete.