├── README.markdown └── main.go /README.markdown: -------------------------------------------------------------------------------- 1 | # init 2 | 3 | Minimal init(8) for use with Docker. Wraps your command 4 | to reap zombie processes. 5 | 6 | Heavily influenced by [Xe/init](https://github.com/Xe/init) 7 | 8 | ## Installation 9 | 10 | $ go get github.com/ddollar/init 11 | 12 | ## Usage 13 | 14 | $ init /path/to/subcommand args 15 | 16 | ## License 17 | 18 | Apache 2.0 © 2015 David Dollar 19 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "log" 7 | "os" 8 | "os/exec" 9 | "os/signal" 10 | "syscall" 11 | "time" 12 | ) 13 | 14 | var useUsr1 *bool 15 | 16 | func reap() { 17 | syscall.Wait4(-1, nil, syscall.WNOHANG, &syscall.Rusage{}) 18 | } 19 | 20 | func terminate() { 21 | if *useUsr1 { 22 | syscall.Kill(-1, syscall.SIGUSR1) 23 | time.Sleep(2 * time.Second) 24 | } 25 | 26 | syscall.Kill(-1, syscall.SIGTERM) 27 | time.Sleep(5 * time.Second) 28 | 29 | syscall.Kill(-1, syscall.SIGKILL) 30 | os.Exit(0) 31 | } 32 | 33 | func handleSignal(sig os.Signal, handler func()) { 34 | ch := make(chan os.Signal, 1) 35 | signal.Notify(ch, sig) 36 | 37 | for _ = range ch { 38 | handler() 39 | } 40 | } 41 | 42 | func main() { 43 | go handleSignal(syscall.SIGCHLD, reap) 44 | go handleSignal(syscall.SIGTERM, terminate) 45 | go handleSignal(syscall.SIGKILL, terminate) 46 | 47 | useUsr1 = flag.Bool("usr1", false, "send usr1 before term") 48 | 49 | flag.Parse() 50 | 51 | args := flag.Args() 52 | 53 | if len(args) < 1 { 54 | fmt.Println("usage: init ") 55 | os.Exit(1) 56 | } 57 | 58 | cmd := exec.Command(args[0], args[1:]...) 59 | cmd.Stdin = os.Stdin 60 | cmd.Stdout = os.Stdout 61 | cmd.Stderr = os.Stderr 62 | err := cmd.Run() 63 | 64 | if err != nil { 65 | log.Fatal(err) 66 | } 67 | } 68 | --------------------------------------------------------------------------------