Last active
August 29, 2015 14:15
-
-
Save ellotheth/9c795ab95df0939cb3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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