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