I want to run another binary from go app with something like:
cmd := exec.Command("another_app_binary", "-config", "config.conf")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
When I kill the parent process(code above), child process(another_app_binary) becomes orphan. What options do I have to cleanup child processes after parent exits/TERMINATES? It has to be somewhat platform nutural because I plan to run it on win/linux.
You can use the os/signal package to listen for the kill signal in your current process.
import (
"os/exec"
"os"
"os/signal"
)
// ...
cmd := exec.Command("another_app_binary", "-config", "config.conf")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, os.Kill)
go func() {
<-c
// cleanup
cmd.Process.Kill()
os.Exit(1)
}()
// ...