A process cannot intercept all signals. Specifically, on UNIX-like systems, the
syscall.SIGKILL
and syscall.SIGSTOP
signals cannot be captured by the
process but handled directly by the kernel. It is, therefore, pointless
to try and handle these signals.
func main() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c,
os.Interrupt,
syscall.SIGKILL, // cannot be captured
syscall.SIGSTOP, // cannot be captured
)
// Block until a signal is received.
s := <-c
fmt.Println("Got signal:", s)
}
func main() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block until a signal is received.
s := <-c
fmt.Println("Got signal:", s)
}