GO监听退出信号

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
)

func main() {
  
    sigs := make(chan os.Signal,1)
    done := make(chan bool,1)
    signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
    go func() {
        sig := <-sigs
        fmt.Println()
        fmt.Println(sig)
        done <- true
    }()

    fmt.Println("awaiting signal")
    <-done
    fmt.Println("exiting")
}

你可能感兴趣的:(GO监听退出信号)