An example / demo project for managing a child process in the same way across Windows/Mac/Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
813 B

// +build !windows
package main
import (
"os"
"os/signal"
"golang.org/x/sys/unix"
)
var processAttributesOSIndependent = &unix.SysProcAttr{Setpgid: true}
func terminateProcessOSIndependent(pid int, signal os.Signal) error {
if pid == 0 || pid == -1 {
return nil
}
pgid, err := unix.Getpgid(pid)
if err != nil {
return err
}
if pgid == pid {
pid = -1 * pid
}
target, err := os.FindProcess(pid)
if err != nil {
return err
}
return target.Signal(signal)
}
// kills the process with pid pid, as well as its children.
func killProcessOSIndependent(process *os.Process) error {
return unix.Kill(-1*process.Pid, unix.SIGKILL)
}
func getSignalChannelOSIndependent() <-chan os.Signal {
sc := make(chan os.Signal, 10)
signal.Notify(sc, unix.SIGTERM, unix.SIGINT, unix.SIGHUP)
return sc
}