Documentation
¶
Overview ¶
shutdown is a simple package for creating a context that will be canceled if your program receives SIGTERM or SIGINT from the os
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureInterrupts ¶
CaptureInterrupts is the same as context.WithCancel, but the returned context will cancel if your program receives an interrupt from the os
Example ¶
package main
import (
"context"
"fmt"
"syscall"
"time"
"github.com/jtwatson/shutdown"
)
func main() {
// Capture interrupt so we can handle them gracefully.
ctx, cancel := shutdown.CaptureInterrupts(context.Background())
defer cancel()
fmt.Println("Waiting for interrupt")
go func() {
time.Sleep(time.Millisecond * 100)
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
<-ctx.Done()
fmt.Println("Received interrupt")
}
Output: Waiting for interrupt Received interrupt
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.