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.

30 lines
475 B

9 years ago
11 years ago
  1. // +build !plan9
  2. package main
  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. go func() {
  22. for _ = range signalChan {
  23. fn()
  24. os.Exit(0)
  25. }
  26. }()
  27. }