From b6e969be7a69a638a73618f45ae7a5f40f1157ac Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 19 Jun 2017 16:43:23 +0200 Subject: pkg/osutil: add helper function for SIGINT handling --- pkg/osutil/osutil.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'pkg/osutil') diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index a9c64ad1c..cbd287d4d 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -9,6 +9,7 @@ import ( "io" "os" "os/exec" + "os/signal" "path/filepath" "syscall" "time" @@ -75,3 +76,21 @@ func IsExist(name string) bool { _, err := os.Stat(name) return err == nil || !os.IsNotExist(err) } + +// HandleInterrupts closes shutdown chan on first SIGINT +// (expecting that the program will gracefully shutdown and exit) +// and terminates the process on third SIGINT. +func HandleInterrupts(shutdown chan struct{}) { + go func() { + c := make(chan os.Signal, 3) + signal.Notify(c, syscall.SIGINT) + <-c + close(shutdown) + fmt.Fprint(os.Stderr, "SIGINT: shutting down...\n") + <-c + fmt.Fprint(os.Stderr, "SIGINT: shutting down harder...\n") + <-c + fmt.Fprint(os.Stderr, "SIGINT: terminating\n") + os.Exit(int(syscall.SIGINT)) + }() +} -- cgit mrf-deployment