You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
481 B

9 years ago
11 years ago
  1. // +build !plan9
  2. package util
  3. import (
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. )
  8. func OnInterrupt(fn func()) {
  9. // deal with control+c,etc
  10. signalChan := make(chan os.Signal, 1)
  11. // controlling terminal close, daemon not exit
  12. signal.Ignore(syscall.SIGHUP)
  13. signal.Notify(signalChan,
  14. os.Interrupt,
  15. os.Kill,
  16. syscall.SIGALRM,
  17. // syscall.SIGHUP,
  18. syscall.SIGINT,
  19. syscall.SIGTERM,
  20. // syscall.SIGQUIT,
  21. )
  22. go func() {
  23. for _ = range signalChan {
  24. fn()
  25. os.Exit(0)
  26. }
  27. }()
  28. }