GO

GO监听退出信号

2019-03-14  本文已影响1人  会长__
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")
}

上一篇下一篇

猜你喜欢

热点阅读