From 479ed3f97417d95f19491931d20e696f79f62cc9 Mon Sep 17 00:00:00 2001 From: Guilherme Salazar Date: Fri, 10 Jan 2020 21:00:44 -0800 Subject: [PATCH] feat(*) exit if parent died Run a goroutine every minute to check if parent died and exit. Assumption: if parent dies, the process gets adopted by init (pid 1). --- main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.go b/main.go index 321da09..583b7a0 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "os" "reflect" "fmt" + "time" ) var version = "development" @@ -99,6 +100,10 @@ func startServer() { runServer(listener) } +func isParentAlive() bool { + return os.Getppid() != 1 // assume ppid 1 means process was adopted by init +} + func main() { if *showVersion == true { printVersion() @@ -111,6 +116,17 @@ func main() { } if socket != "" { + go func() { + for { + if ! isParentAlive() { + log.Printf("Kong exited; shutting down...") + os.Exit(0) + } + + time.Sleep(1 * time.Second) + } + }() + startServer() } }