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.

28 lines
393 B

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. signal.Notify(signalChan,
  12. os.Interrupt,
  13. os.Kill,
  14. syscall.SIGALRM,
  15. syscall.SIGHUP,
  16. syscall.SIGINT,
  17. syscall.SIGTERM,
  18. syscall.SIGQUIT)
  19. go func() {
  20. for _ = range signalChan {
  21. fn()
  22. os.Exit(0)
  23. }
  24. }()
  25. }