Skip to content

Instantly share code, notes, and snippets.

@burke
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burke/1c105378ac0629b39485 to your computer and use it in GitHub Desktop.
Save burke/1c105378ac0629b39485 to your computer and use it in GitHub Desktop.
func startReapingChildren() {
sigchld := make(chan os.Signal, 1)
signal.Notify(sigchld, syscall.SIGCHLD)
log.Println("starting to reap children")
go func() {
for _ = range sigchld {
reapChildren()
}
}()
}
func reapChildren() {
var stat syscall.WaitStatus
var rusage syscall.Rusage
for {
// {_,ECHILD} means that there are no children.
// {0,nil} means that there are children, but none have changed state (died)
// {_,any other error} means something kinda serious happened.
pid, err := syscall.Wait4(0, &stat, syscall.WNOHANG, &rusage)
if err != nil {
if err == syscall.ECHILD {
break
}
reportErrorAndDie("Unexpected error reaping children: ", err) // EINVAL
}
if pid == 0 {
break
}
if pid == AppPid {
fmt.Printf("App rusage: %#v\n", rusage)
log.Println("App died")
AppDead <- stat.ExitStatus()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment