Run Command in Go and Detach It from Process
Problem:
You need to execute a long-running process in Go and ensure that:
Attempted Solution:
You tried using cmd.Start() and cmd.Process.Release(), but they don't prevent the process from becoming a zombie when your program exits. You also want the process to be as separate as possible, with its own parent PID, group PID, etc.
Solution:
The problem you're facing is that Linux doesn't allow you to change the parent of a process once it's started. If you want a truly detached process, it needs to be started by something else, such as atd or systemd.
To address the issues with zombies and process termination, you can use a library like [go-reap](https://github.com/hashicorp/go-reap). It provides a Reaper service that automatically reaps orphaned child processes and reports their exit status.
Here's an example using go-reap to start a detached process:
<code class="go">package main import ( "context" "fmt" "os/exec" "github.com/hashicorp/go-reap" ) func main() { // Start a Reaper service to reap orphaned processes. doneCh := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) reaper := reap.NewReaper(cancel) go reaper.Run(ctx) defer reaper.Stop() // Execute a command in a detached process. cmd := exec.Command("my-long-running-command", "args") pid, err := reaper.Reap(cmd) if err != nil { fmt.Printf("Error reaping process: %v\n", err) return } fmt.Printf("PID of detached process: %v\n", pid) // Wait for the detached process to complete. exitStatus, err := reaper.WaitPid(pid) if err != nil { fmt.Printf("Error waiting for process: %v\n", err) return } fmt.Printf("Detached process exited with status: %v\n", exitStatus) // Stop the Reaper service when done. close(doneCh) }</code>
By using go-reap and reaping the process when it exits, you can ensure that it doesn't become a zombie and that you can track its exit status.
The above is the detailed content of How to Detach a Long-Running Process in Go and Avoid Zombies?. For more information, please follow other related articles on the PHP Chinese website!