os/signal
Go's standard library package for receiving operating system signals, such as SIGTERM, on a channel instead of being killed by them.
In more detail
signal.Notify(ch, syscall.SIGTERM) delivers the signal to ch; give the channel a buffer of one so no signal is dropped. signal.NotifyContext returns a context that is cancelled when the signal arrives, which fits neatly with shutdown code.
Example
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM)Related terms
- SIGTERM — The signal that politely asks a process to finish its work and exit.
- signal — A short notification the kernel delivers to a process, such as please stop or stop now.
- graceful shutdown — Stopping a service in an orderly way: finish current work, close connections, then exit.
- channel — A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
- system call — A request a program makes to the kernel, such as open this file or start a new process.
Taught in
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go