2014-05-13 22:04:04 +00:00
|
|
|
// +build !plan9
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OnInterrupt(fn func()) {
|
|
|
|
// deal with control+c,etc
|
|
|
|
signalChan := make(chan os.Signal, 1)
|
2015-10-20 05:03:18 +00:00
|
|
|
// controlling terminal close, daemon not exit
|
2015-10-19 06:39:11 +00:00
|
|
|
signal.Ignore(syscall.SIGHUP)
|
2014-05-13 22:04:04 +00:00
|
|
|
signal.Notify(signalChan,
|
|
|
|
os.Interrupt,
|
|
|
|
os.Kill,
|
2014-05-14 02:26:05 +00:00
|
|
|
syscall.SIGALRM,
|
2015-10-19 06:39:11 +00:00
|
|
|
// syscall.SIGHUP,
|
2014-05-13 22:04:04 +00:00
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT)
|
|
|
|
go func() {
|
|
|
|
for _ = range signalChan {
|
|
|
|
fn()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|