-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.go
51 lines (47 loc) · 951 Bytes
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Micro init process to run zero downtime servers within containers
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage:\n\t%s COMMAND [args...]\n", os.Args[0])
os.Exit(1)
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
child, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
log.Fatal(err)
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
sig := make(chan os.Signal)
signal.Notify(sig)
signal.Ignore(syscall.SIGCHLD)
go func() {
for {
syscall.Kill(-(child), (<-sig).(syscall.Signal))
}
}()
var status syscall.WaitStatus
syscall.Wait4(-1, &status, 0, nil)
}