Add utility to start a child process reaper

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
Mrunal Patel 2016-08-15 15:57:13 -07:00
parent 6b2b1ee576
commit 01e9ac5313

View file

@ -5,9 +5,12 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/Sirupsen/logrus"
)
const PR_SET_CHILD_SUBREAPER = 36
@ -97,3 +100,26 @@ func dockerRemove(container string) error {
}
return nil
}
// StartReaper starts a goroutine to reap processes
func StartReaper() {
logrus.Infof("Starting reaper")
go func() {
sigs := make(chan os.Signal, 10)
signal.Notify(sigs, syscall.SIGCHLD)
for {
// Wait for a child to terminate
sig := <-sigs
logrus.Infof("Signal received: %v", sig)
for {
// Reap processes
cpid, _ := syscall.Wait4(-1, nil, syscall.WNOHANG, nil)
if cpid < 1 {
break
}
logrus.Infof("Reaped process with pid %d", cpid)
}
}
}()
}