Skip to content

Instantly share code, notes, and snippets.

@ellotheth
Last active August 29, 2015 14:15
Show Gist options
  • Save ellotheth/9c795ab95df0939cb3b8 to your computer and use it in GitHub Desktop.
Save ellotheth/9c795ab95df0939cb3b8 to your computer and use it in GitHub Desktop.
func killProcessTree(parent int) {
// get child processes
out, _ := exec.Command("ps", "-o", "pid", "--no-heading", "--ppid", strconv.Itoa(parent)).Output()
// if the child processes have any children, get them first
for _, child := range strings.Split(string(out), "\n") {
if pid, _ := strconv.Atoi(strings.TrimSpace(child)); pid > 0 {
killProcessTree(pid)
}
}
// kill the process
if process, err := os.FindProcess(parent); err != nil {
log.Fatal("Process not found ", err)
} else {
if err := process.Kill(); err != nil {
log.Fatal("Can't kill ", parent, err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment