publish signals
This commit is contained in:
parent
973ace22a0
commit
031797c22f
@ -66,7 +66,7 @@ Line Filters
|
|||||||
Environment Variables
|
Environment Variables
|
||||||
Spawning Processes
|
Spawning Processes
|
||||||
Exec'ing Processes
|
Exec'ing Processes
|
||||||
# Signals
|
Signals
|
||||||
# Exit
|
# Exit
|
||||||
# HTTP Client
|
# HTTP Client
|
||||||
# HTTPS Client
|
# HTTPS Client
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
// Sometines we'd like our Go programs to intelligently
|
// Sometines we'd like our Go programs to intelligently
|
||||||
// handle Unix signals. For example, we might want a
|
// handle [Unix signals](http://en.wikipedia.org/wiki/Unix_signal).
|
||||||
// server to gracefully shutdown when it receives a
|
// For example, we might want a server to gracefully
|
||||||
// `SIGTERM`, or a command-line tool to stop processing
|
// shutdown when it receives a `SIGTERM`, or a command-line
|
||||||
// input if it receives a `SIGINT`. Here's how to handle
|
// tool to stop processing input if it receives a `SIGINT`.
|
||||||
// signals in Go with channels.
|
// Here's how to handle signals in Go with channels.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ import "os/signal"
|
|||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Go signal notification works by sending `os.Signal`
|
// Go signal notification works by sending `os.Signal`
|
||||||
// values on a channel. We'll create a channel to
|
// values on a channel. We'll create a channel to
|
||||||
// receive these notifications (we'll also make one to
|
// receive these notifications (we'll also make one to
|
||||||
@ -23,17 +24,20 @@ func main() {
|
|||||||
// `signal.Notify` registers the given channel to
|
// `signal.Notify` registers the given channel to
|
||||||
// receive notifications of the specified signals.
|
// receive notifications of the specified signals.
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
// This goroutine executes a blocking receive for
|
||||||
|
// signals. When it gets one it'll print it out
|
||||||
|
// and then notify the program that it can finish.
|
||||||
go func() {
|
go func() {
|
||||||
// This goroutine makes a blocking receive for
|
|
||||||
// signals. When it gets one it'll print it out
|
|
||||||
// and then notify the program that it can finish.
|
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println(sig)
|
fmt.Println(sig)
|
||||||
done <- true
|
done <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// The program will wait here until it gets the
|
// The program will wait here until it gets the
|
||||||
// expected signal, and then exit.
|
// expected signal (as indicated by the goroutine
|
||||||
|
// above sending a value on `done`) and then exit.
|
||||||
fmt.Println("awaiting signal")
|
fmt.Println("awaiting signal")
|
||||||
<-done
|
<-done
|
||||||
fmt.Println("exiting")
|
fmt.Println("exiting")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user