A Go package that returns a context.Context which is cancelled when the process receives SIGINT or SIGTERM, to shut programs down gracefully.
go get github.com/aklinkert/go-exitcontextRequires Go 1.21 or newer.
If you already have a context and want to recycle that one:
ctx := context.Background()
exitCtx := exitcontext.NewWithContext(ctx)else just use New, which creates a context.Background() under the hood:
exitCtx := exitcontext.New()
<-exitCtx.Done() // SIGINT or SIGTERM receivedThe signal handler is released as soon as the context is done, whether a signal arrived or the parent context was
cancelled. A second SIGINT (Ctrl-C) or SIGTERM after that gets Go's default behaviour and terminates the process,
same as with signal.NotifyContext.
For new code, the standard library's signal.NotifyContext offers the same with an explicit stop function.
MIT, see LICENSE.