terminator/terminator.go
2024-04-11 11:07:26 +02:00

35 lines
742 B
Go

// Package terminator provides a simple way to await process termination.
//
// Just wait until the Terminate channel closes:
//
// <-terminator.Terminate
//
// it will fall through, once your process receives Ctrl-C, SIGINT, SIGTERM or SIGKILL signal.
package terminator
import (
"os"
"os/signal"
"syscall"
)
// enable to react to
//
// <-terminator.Terminate
//
// nicely
var Terminate = make(chan struct{})
func init() {
sigs := make(chan os.Signal)
signal.Notify(sigs, os.Interrupt, os.Kill)
signal.Notify(sigs, syscall.SIGTERM)
// When notified about SIGINT, SIGTERM, or SIGKILL close the Terminator channel
// in order to notify interested parties to quit.
go func(c <-chan os.Signal) {
<-c
close(Terminate)
}(sigs)
}