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
383 B

10 years ago
  1. package common
  2. import (
  3. "fmt"
  4. "os"
  5. "os/signal"
  6. )
  7. func TrapSignal(cb func()) {
  8. c := make(chan os.Signal, 1)
  9. signal.Notify(c, os.Interrupt)
  10. signal.Notify(c, os.Kill)
  11. go func() {
  12. for sig := range c {
  13. fmt.Printf("captured %v, exiting...\n", sig)
  14. if cb != nil {
  15. cb()
  16. }
  17. os.Exit(1)
  18. }
  19. }()
  20. select {}
  21. }
  22. func Exit(s string) {
  23. fmt.Printf(s + "\n")
  24. os.Exit(1)
  25. }