Golang library for monitoring asynchronously signals.
$ go get github.com/bluele/gsignal
package main
import (
"github.com/bluele/gsignal"
"log"
"os"
"syscall"
"time"
)
func main() {
watcher := gsignal.NewWatcher()
watcher.Watch(func(sig os.Signal) {
log.Println("Catch signal: ", sig)
os.Exit(0)
}, os.Interrupt, syscall.SIGTERM)
watcher.Watch(func(sig os.Signal) {
log.Println("Ignore signal: ", sig)
}, syscall.SIGHUP)
// start watching specified signal numbers.
watcher.Run()
log.Println("PID: ", os.Getpid())
log.Println("Sleep...")
time.Sleep(time.Minute)
}
$ go run example.go
2015/01/04 14:30:40 PID: 96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Catch signal: interrupt
$ kill -SIGTERM <your pid>
2015/01/04 14:30:40 PID: 96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Catch signal: terminated
$ kill -SIGHUP <your pid>
2015/01/04 14:30:40 PID: 96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Ignore signal: hangup
$ go test
Jun Kimura