terminal.go 513 B

123456789101112131415161718192021
  1. package utils
  2. import (
  3. "context"
  4. log "github.com/sirupsen/logrus"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. )
  10. func CloseTerminal() {
  11. // 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
  12. quit := make(chan os.Signal)
  13. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  14. <-quit
  15. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  16. defer cancel()
  17. log.Println("- EXIT - [Ctrl+C] The project will automatically close after 3 seconds")
  18. time.Sleep(time.Second * 3)
  19. }