Last active
November 2, 2018 16:25
-
-
Save williammartin/5caf8d5304151a13e7c1707537f2421a to your computer and use it in GitHub Desktop.
subreaper example
This file contains 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
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"time" | |
"github.com/williammartin/subreaper" | |
) | |
func main() { | |
switch os.Args[1] { | |
case "run": | |
parent() | |
case "child": | |
child() | |
case "grandchild": | |
grandchild() | |
default: | |
panic("You must pass an argument") | |
} | |
} | |
func parent() { | |
must(subreaper.Set()) | |
isSubreaper, err := subreaper.IsSubreaper() | |
mustNot(err) | |
fmt.Printf("Is this process a subreaper?: %t\n", isSubreaper) | |
cmd := exec.Command("/proc/self/exe", "child") | |
must(cmd.Run()) | |
time.Sleep(time.Second * 60) | |
} | |
func child() { | |
cmd := exec.Command("/proc/self/exe", "grandchild") | |
must(cmd.Start()) | |
} | |
func grandchild() { | |
time.Sleep(time.Second * 20) | |
} | |
func must(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func mustNot(err error) { | |
must(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment