github.com/Mrs4s/go-cqhttp@v1.2.0/global/signal_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package global
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  	"sync"
    10  	"syscall"
    11  )
    12  
    13  // SetupMainSignalHandler is for main to use at last
    14  func SetupMainSignalHandler() <-chan struct{} {
    15  	mainOnce.Do(func() {
    16  		mainStopCh = make(chan struct{})
    17  		mc := make(chan os.Signal, 4)
    18  		closeOnce := sync.Once{}
    19  		signal.Notify(mc, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGUSR1)
    20  		go func() {
    21  			for {
    22  				switch <-mc {
    23  				case os.Interrupt, syscall.SIGTERM:
    24  					closeOnce.Do(func() {
    25  						close(mainStopCh)
    26  					})
    27  				case syscall.SIGQUIT, syscall.SIGUSR1:
    28  					dumpStack()
    29  				}
    30  			}
    31  		}()
    32  	})
    33  	return mainStopCh
    34  }